Skip to main content
remove-character-from-string-in-js

JavaScript Remove Certain Characters from String

in this post, we’ll learn Remove Character From String In JavaScript.Let’s look at all the different ways to delete a character from a string in JavaScript.

Remove character from String in JavaScript

Let’s remove character from a string in javascript. You can use one of the following methods:

  1. substr() – It helps to removes a character from a particular index in the String.
  2. replace() – The replaces a specific character/string with another character/string.
  3. slice() – This method help tp extracts parts of a string between the given parameters.
  4. replace() method with a regular expression.

JavaScript String replace()

The replace() function replaces the specific character/string with another character/string. It returns the new String with the pattern replaced by the replacement.

The Syntax:

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

The replace() method takes two parameters, the first is the String to be replaced, and the second is the String, which is to be replaced with.

str = 'Hi! I m pythonpip blog.';

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

console.log('Original String: ', str);
console.log('Final String: ', newStr);

Output:

Original String:  Hi! I m pythonpip blog.
Final String:  Hi! I m tythontit blog.

replace() method with a regular expression

This method is used to replace string using regular expression.Unlike the preceding method, this function is used to remove all occurrences of a specific character.

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

newStr = str.replace(/t/g, '');
console.log('Final String: ', newStr);

Output:

Original String:  pythonpip
Final String:  pyhonpip

Removing character from string using slice()

The JavaScript String slice() is extracted parts of a string between two arguments. The slice() method accepts the string’s starting and ending indexes and returns the string in between them.

If no ending index is supplied, the string’s length is assumed. The string ending index is to be one less than the length of the string.

Set the beginning index to 1 to remove the first character from the string. It creates a string starting with the second character and ending with the last character.

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

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

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

Output:

Original String:  pythonpip
Removing the first character ythonpip
Removing the last character:  pythonpi

Removing a specific character using substr()

The Substring() is a built-in Javascript function that returns a portion of a string between the start and end indexes or to the end of a string.

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

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

Output:

Original String: pythonpip
Removing the first character: ythonpip

Conclusion

The str.substr(), str.slice(), and str.replace() methods can be used to remove characters from the beginning, middle, and end of a string, respectively.

Leave a Reply

Your email address will not be published. Required fields are marked *