JavaScript - How to Remove Character From A String.


JavaScript - How to Remove Character From A String.

In this article we will see how to remove character from a string in JavaScript.

What is a String?

In general, a string is define as a sequence of characters in a programming language. It can be a word or a sentence.

In JavaScript, there are lots of methods and properties for string manipulation. Using these methods we can transform a string, do searches on a string, replace or remove character from a string.

In this article, we will use some in-built JavaScript methods like replace(), split() and join() to remove a specific character from a string.

So let's check each method with an example to remove a character from string in JavaScript.

Method 1: Using replace() method

The replace() method in JavaScript is use to search a value or a regular expression in a string and replace it with a given replacement character.

It return us a new string with the replaced word.

Syntax:

replace(searchCharacter, replaceCharacter)

As per the definition, the replace() method is use to replace character, however we can also use it to remove character in a string too.

We just have to change the replaceCharacter to an empty string ('').

Lets see it in action:

let text = 'I have a cat';
// search word is 'a' and replace Character is empty string ' '
let removeChar = text.replace('a', ''); 

console.log(removeChar);

Output:

I hve a cat

As you can see in the output, the replace() have only replace the first occurrence of the character 'a' in the string. This is because replace() method stops the search at the first occurrence of the character and do not continue the search afterwards.

Remove Character From a String Using Regular Expression

So to solve this issue, we will have to use the regular expression with the replace() method.

Lets see an example:

let text = 'I have a cat';
// search word is /a/g (regular expression) and replace Character is empty string ' '
let removeChar = text.replace(/a/g, ''); 

console.log(removeChar);

Output:

I hve  ct

When we use a regular expression with the g modifier, it check for the searchCharacter (here the character a) in the whole string and remove all the occurrence of the character.

Method 2 : Using split() and join() method

The split() method truncate (divide) a string into substrings depending on its splitter ( divider ). And a splitter can be just a character or a regular expression.

After splitting the string , it returns us an array with the substrings.

Example:

let text = 'I have a cat';
// split the string with splitter as letter 'a'
let removeChar = text.split('a'); 

console.log(removeChar); // [ 'I h', 've ', ' c', 't' ]

The join() method in JavaScript helps us to concatenate all the elements in the array together with a separator. It joins the array and return us a string.

Example:

let array = ['one','two','three']

let joinArr = array.join("+")
console.log(joinArr) // one+two+three

So, now using the split and join method, we can remove character in a string using split() method and then join it together using join() method with an empty string ('') in join as a separator.

Example:

We will remove the character 'a' from the string ('I have a cat')

let text = 'I have a cat';
// split the string with splitter as letter 'a' and join it with and empty string ("")
let removeChar = text.split('a').join('') 

console.log(removeChar);

Output:

I hve  ct

That's it, this is how you can remove a character from a string using JavaScript.

Related Topics:

JavaScript - String Replace All Appearances

How To Remove Last Character From A String In Javascript

Remove Whitespace From JavaScript String

Ways To Combine Multiple Strings Into A Single String | Concatenate Strings

Get The 10 Characters From A String Using Javascript.

How to compare two strings in JavaScript?