We can use replace() function to remove the character string. It is one of the best and easiest methods. The replace() function is used to replace the selected character with a given character in the string. We can pass two parameters of replace() function, the search string, and the new string.
stringvariable.replace(search value, new value);
var name = "Wonder Develop"; document.write("Before Removed Character : "+name+"<br/>"); var name = name.replace("W"," "); document.write("After Removed Character : "+name);
- See the above example code, we have created a variable “name” and assigned a string value.
- I want to remove the “W” letter in the “name” string. So, we use replace() function.
- To use replace() function, we must put a string variable called “name” then put the (.) dot operator and then use replace() function. Ex: name.replace().
- To remove characters, we must pass two parameters. First, pass what character you want to remove and then pass an empty string.
- See the above output, we remove the character “W” successfully.
Learn also: How to remove the first character from string >> click here