AppDividend
Latest Code Tutorials

JavaScript: Remove Character from String Example

0

A standard form of string manipulation in JavaScript is to remove the character from the string. Let’s explore all of the ways we can do remove the character from string in JavaScript.

JavaScript: Remove character from String

There are different methods and techniques you can use to remove a character from String:

  1. replace – It replaces a specific character/string with another character/string.
  2. slice – The slice() method is used to extract parts of a string between the given parameters.
  3. substr – It removes a character from a particular index in the String.
  4. Use the string replace() function with a regular expression.

JavaScript String replace()

Javascript String replace() is an inbuilt method that returns the new String with some or all matches of the pattern replaced by the replacement.

The replace() function is used to replace the specific character/string with another character/string. It takes two parameters, first is the String to be replaced and the second is the String, which is to be replaced with.

In this case, the first argument is the character that is to be removed, and the second parameter can be given as the empty string. The replace() method will remove the character from the String. The replace() method removes the first occurrence of the String.

Syntax

string.replace('characterToReplace', '');

Example

// app.js

str = 'Hello cy Adele';

newStr = str.replace('c', '');

console.log('Original String: ', str);
console.log('After character removed: ', newStr);

Output

node app.js
Original String:  Hello cy Adele
After character removed:  Hello y Adele

In this example, our original String is ‘Hello by Adele’, and after using String.replace() and pass the empty character to be replaced for ‘c‘ character, we successfully removed the ‘c‘ character, and output is ‘Hello y Adele’.

Using a replace() method with a regular expression

To remove a character from a string, use the combination of string replace() and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function.

A regular expression is used instead of a string along with global property. It will select every occurrence in a string and it can be removed.

See the following code.

// app.js

str = 'AppDividend';
console.log('Original String: ', str);

newStr = str.replace(/D/g, '');
console.log('After character removed: ', newStr);

Output

node app.js
Original String:  AppDividend
After character removed:  Appividend

You can see that using regular expression and replace() method, we have replaced D character with nothing and it has removed the character from the string.

Removing character from string using slice()

JavaScript String slice() function is used to extract parts of a string between the given parameters. The slice() method takes the starting index and the ending index of the string and returns the string in between these indices.

If an ending index is not specified, it is assumed to be the length of the string.

To remove the first character from the string, specify the beginning index to 1. It extracts a string from the second character up to the end of the string.

To remove the last character from the string, specify the ending index to be one less than the length of the string. This extracts a string from the beginning of the string to the second to the last character.

See the following code.

// app.js

str = 'AppDividend';
console.log('Original String: ', str);

removeFirstChar = str.slice(1);
console.log('Removing the first character', removeFirstChar);

removeLastChar = str.slice(0, str.length - 1);
console.log('Removing the last character: ', removeLastChar);

Output

Original String:  AppDividend
Removing the first character ppDividend
Removing the last character:  AppDividen

In this example, we have removed the first and the last character of a string using the slice() method.

To remove the first character, we have passed 1 as an argument.

To remove the last character, we have passed the first argument as 0 and second as string length – 1.

Removing a specific character using substr()

Javascript string substring() is an inbuilt function that returns a part of the string between the start and end indexes, or to the end of a string.

The substr() method can be used to remove a character from the specific index in the string. The substr() function is used to extract the parts of the string between the given parameters.

The substr() method takes two parameters, first is the starting index and the second is the ending index of the string. The function returns the string between these indices is returned.

The portion of the string before and after the character to be removed is separated and joined together. This removes a character from a particular index.

Example

To remove the first character from a string, pass the first parameter as 1 and the second parameter as string.length. It will return the string whose first character is stripped.

// app.js

str = 'AppDividend';
console.log('Original String:', str);

newStr = str.substr(1, str.length);
console.log('After removing the first character:', newStr);

Output

node app.js
Original String: AppDividend
After removing the first character: ppDividend

You can see from the output that the first character is removed.

Conclusion

To remove a character from the start of the string, middle of the string, and at the end of a string, use the str.substr(), str.slice(), and str.replace() methods.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.