Remove a Character From String in JavaScript
In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:
- Removing unwanted symbols or spaces.
- Keeping only the necessary characters.
- Formatting the text.
Methods to Remove a Character From a String
Below are the following methods by which we can remove the character from a string:
1. Using replace() – Removes First Occurrence
One of the most common methods to remove a character from a string is by using the replace() method. This method searches for a specified character or pattern in a string and replaces it with something else, which can be an empty string (“”) to remove the character.
Syntax
string.replace(searchValue, newValue);
In the above syntax:
searchValue
: The character, string, or regular expression to search for.newValue
: The value to replace thesearchValue
with (an empty string""
to remove the character).
let s = "GeeksForGeeks";
res = s.replace("G", "");
console.log(res);
Output
eeksForGeeks
2. Using replace() with a Regular Expression
This method removes all occurrences of a specified character or string from the input, unlike the previous one, which only removes the first occurrence. It uses a regular expression with the global flag to target and remove every instance.
Syntax
string.replace(/pattern/g, '');
In this syntax:
- /pattern/g: The regular expression with the g flag (global) to match all occurrences of the specified character or string.
- ”: The replacement string (empty string “” to remove the matched characters).
s = "GeeksForGeeks";
res = s.replace(/G/g, "");
console.log(res);
Output
eeksForeeks
3. Using substring() Method
The substring()
method works similarly to slice()
, but it does not support negative indices. It extracts a portion of a string between two specified indices.
Syntax
string.substring(startIndex, endIndex);
In the above syntax
- startIndex: The starting position of the substring (inclusive).
- endIndex: The ending position of the substring (exclusive).
let str = "Hello World";
let newStr = str.substring(0, 4) + str.substring(5);
console.log(newStr);
Output
Hell World
4. Using slice() Method
The slice() method is similar to substring(), but it allows using negative indices to refer to positions from the end of the string. It’s useful for more flexible string manipulation.
Syntax
string.slice(startIndex, endIndex);
In the above syntax
- startIndex: The starting position of the slice.
- endIndex: The ending position of the slice (optional).
let str = "Hello World";
let newStr = str.slice(0, 4) + str.slice(5);
console.log(newStr);
Output
Hell Wrld
5. Using split() and join()
The split()
method divides a string into an array of substrings, and the join()
method joins an array back into a string. This approach uses the split() and join() methods.
Syntax
string.split(separator).join(newString);
In the above syntax
- separator: The character or string to split the original string by.
- newString: The string to use when joining the array elements (use an empty string “” to join without anything).
let s = "GeeksForGeeks";
let res = s.split("G").join("");
console.log(res);
Output
eeksForeeks
6. Using Array.filter()
This method converts the string into an array of characters, use the filter()
method to remove specific characters, and then join the array back into a string.
let s = "GeeksForGeeks";
let c = 'G';
let res = Array.from(s)
.filter(char => char !== c)
.join('');
console.log(res);
Output
eeksForeeks
Conclusion
Removing characters from a string in JavaScript can be done in several ways, depending on the situation. Whether using replace(), split() and join(), substring(), slice(), or advanced methods like filter(), each approach is suited to specific needs.