4 Effective Ways to Remove Character from String using JavaScript
Looking to remove the character from string in JavaScript? Let’s discuss remove method details in this post.
Contents
Using substring() method
JavaScript substring() method retrieves the characters between two indexes and returns a new substring.
Two indexes are nothing but startindex and endindex.
Let’s try to remove the first character from the string using the substring method in the below example.
function removeFirstCharacter() {
var str = 'tracedynamics';
str = str.substring(1);
console.log(str);
}
Output:
racedynamics
Now let’s remove the last character from the string using the substring method in the below example.
function removeLastCharacter() {
var str = 'tracedynamics';
str = str.substring(0,str.length-1);
console.log(str);
}
Output:
tracedynamic
the length property is used to determine the last element position.
As per the output above, you can see that specified first and last characters are removed from the original string.
With substr() method
substr() method will retrieve a part of the string for the given specified index for start and end position.
Let’s remove the first character from string using substr function in the below example.
function removeFirstCharacter() {
var str = 'tracedynamics';
str = str.substr(1);
console.log(str);
}
Output:
racedynamics
Now let’s see how to remove the last character from string using substr function in the below example.
function removeLastCharacter() {
var str = 'tracedynamics';
str = str.substr(0,str.length-1);
console.log(str);
}
Output:
tracedynamic
using below JavaScript code, we can also remove whitespace character from a string.
function removeWhiteSpaceCharacter() {
var str = 'tracedynamics ';
str = str.substr(0,str.length-1);
console.log(str);
}
Output:
tracedynamics
As you can see from the above function, in the input string value there is whitespace at the end of the string which is successfully removed in the final output.
Using slice() method
slice() method retrieves the text from a string and delivers a new string.
Let’s see how to remove the first character from the string using the slice method.
function removeFirstCharacter() {
var str = 'tracedynamics';
str = str.slice(1);
console.log(str);
}
Output:
racedynamics
Now let’s remove the last character from string using the slice method.
function removeLastCharacter() {
var str = 'tracedynamics';
str = str.slice(0,str.length-1);
console.log(str);
}
Output:
tracedynamic
Using replace() method
replace() method is used to replace a specified character with the desired character.
This method accepts two arguments or parameters.
The first argument is the current character to be replaced and the second argument is the new character which is to be replaced on.
Let’s see how to replace the first character in a string using the replace function.
function replaceFirstCharacter() {
var str = 'tracedynamics';
str = str.replace('t','T');
console.log(str);
}
Output:
Tracedynamics
Now let’s replace the last character in JavaScript string using replace function.
function replaceLastCharacter() {
var str = 'tracedynamics';
str = str.replace('s','S');
console.log(str);
}
Output:
tracedynamicS
Now let’s replace specified character in a string using the replace method.
function replaceCharacter() {
var str = 'tracedynamics';
str = str.replace('d','D');
console.log(str);
}
Output:
traceDynamics
Also we can apply regular expression(regex)in the replace method to replace any complex character or special character in the string.
Regular expressions(regex) are also useful when dealing with a line break, trailing whitespace, or any complex scenarios.
Using above JavaScript methods, we can also remove characters on string array, line break, trailing whitespace, empty string, Unicode character, double quotes, extra spaces, char, parenthesis, backslash
We can remove multiple characters by giving the specified index for start and end position.
To conclude this tutorial, we covered various types of implementation to remove a character from string using JavaScript.