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.
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.
let s = "GeeksForGeeks";
res = s.replace("G", "");
console.log(res);
Syntax
string.replace(searchValue, newValue);searchValue: The character, string, or regular expression to search for.newValue: The value to replace thesearchValuewith (an empty string""to remove the character).
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.
s = "GeeksForGeeks";
res = s.replace(/G/g, "");
console.log(res);
Syntax
string.replace(/pattern/g, '');- /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).
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.
let str = "Hello World";
let newStr = str.substring(0, 4) + str.substring(5);
console.log(newStr);
Syntax
string.substring(startIndex, endIndex);- startIndex: The starting position of the substring (inclusive).
- endIndex: The ending position of the substring (exclusive).
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.
let str = "Hello World";
let newStr = str.slice(0, 4) + str.slice(5);
console.log(newStr);
Syntax
string.slice(startIndex, endIndex);- startIndex: The starting position of the slice.
- endIndex: The ending position of the slice (optional).
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.
let s = "GeeksForGeeks";
let res = s.split("G").join("");
console.log(res);
Syntax
string.split(separator).join(newString);- 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).
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