JavaScript – How to Remove Character From A String.

featured Image

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

What is a String?

In general, a string is defined 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 characters from a string.

Remove Characters from a String

In this article, we will use some in-built JavaScript methods to remove a specific character from a string.

  1. Using replace() method
  2. Using split() and join() method
  3. Using the splice() method
  4. Using the substring() method

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

Using replace() method

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

It returns us a new string with the replaced word.

Syntax:

replace(searchCharacter, replaceCharacter)

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

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

Let’s 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 checks for the searchCharacter (here the character a) in the whole string and removes all the occurrences of the character.

Using split() and join() to remove character

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

Remove character from string using slice()

The slice() method is javascript is used to extract a part from a string within specific parameters (indices).

We have to pass the start and end index as the parameters to specify which part we want to extract from the string.

It will extract the characters from that part and return the extracted part as a new string.

Syntax:

string.slice(start,end)

In the example below we will remove the first three characters from the word “airplane“.

const str = 'airplane'

const new_str = str.slice(3)

console.log(new_str) //plane

If the end index is not specified, it will be considered to be the length of the string.

Since in the above example, the end was not specified, so it extracted the characters starting from index 3 upto the end of the string.

If you want to remove the last character of the string then we can just specify the end index to be one less than the length of the string.

So now slice will extract the characters from 0 upto the second last character i.e 7 (length of string -1)

const str = 'airplane'

const new_str = str.slice(0,str.length-1)

console.log(new_str) // airplan

And to remove the first character from the string, just pass 1 as the parameter in the slice() method.

const str = 'airplane'

const new_str = str.slice(1)

console.log(new_str) // irplane

It extracts all the characters starting from index 1.

Using substring() to remove characters from a string

The substring() works the same as slice() method, it extracts a part of a string within specified indices.

It does not change the original string and returns the extracted characters as a new string.

It extracts the characters from start to end index (exclusive).

Syntax:

string.substring(start,end)

Let’s remove the first three characters from the string “airplane” using substring() method.

const str = 'airplane'

const new_str = str.substring(3)

console.log(new_str) // plane

To remove the first character we use from the string “airplane”, we use:

const new_str = str.substring(1) //irplane

And, to remove the last character from the string, use:

const new_str = str.substring(0,str.length-1) //airplan

It means extracting all the characters from 0 index upto the second last index .i.e 7 (length of string -1)

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

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?

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more