In this tutorial let’s learn about How to Remove the First Character From String in JavaScript. Because strings are immutable in JavaScript, we can’t remove characters from them in-place. Instead, a new string will be created. In JavaScript, there are three methods for removing the initial character from a string:
Contents
Quick Solution
You can Remove the First Character From String in JavaScript using any of the below methods. Each of them are explained below in detail.
- substring()
- slice()
- replace()
- shift()
Remove First Character From String using substring() Method in JavaScript
The substring() method can be used to remove a character from a string. The substring() function returns a part of a string based on the start and end indexes provided as arguments:
Syntax:
String.substring(start, end);
The method requires the start index, but the end index is optional. When the end index is omitted, the method does not cut the string before the end.
You will only need the first parameter if you wish to eliminate the first character of a string. When the function is called with only one parameter, the characters between the first index and the end of the string are returned.
Code Example : When you only need to delete the first character from a string, use string.substring(1). Here’s an illustration:
let str = 'Python';
str = str.substring(1);
console.log(str);
Output :
ython
If you want to remove first n characters from a string using substring() Method in JavaScript then use:
let str = 'Similar Geeks';
let n=8
str = str.substring(8);
console.log(str);
Output:
Geeks
Remove First Character From String using slice() Method in JavaScript
The JavaScript slice() Method can also be used to remove the first character. This function is equivalent to the substring() method in this scenario. If you pass an index as the first parameter, the function will return your string without the first character.
Syntax:
String.slice(start, end);
The start is mandatory, and the end is optional. If end is not specified, slice() chooses all characters from the string’s beginning to end.
Code Example :
let str = 'JavaScript';
str = str.slice(1);
console.log(str);
Output :
avaScript
If you want to remove first n
characters from a string using slice() Method in JavaScript then use:
let str = 'Similar Geeks';
let n=8
str = str.slice(8);
console.log(str);
Output:
Geeks
Remove First Character From String using replace() Method in JavaScript
The replace() method replaces a part of a string with a new replacement string.
Syntax:
string.replace(old_substring , new_substring)
Replace(/^./, “”) is used here to remove the first character because /^./ is the first character and “” is the empty string.
Code Example :
let str = 'Similar Geeks';
str = str.replace(/^./, "");
console.log(str);
Output :
imilar Geeks
You may also use the replace() method in JavaScript in combination with the charAt() method.
Code Example :
let str = 'Similar Geeks';
const firstChar = str.charAt(0)
str = str.replace(firstChar , "");
console.log(str);
Output :
imilar Geeks
Remove the first character from a string using Shift JavaScript
We’ll convert the string to an array, then utilize the built-in function Shift() Method to shift by one character then use join() to change the array back to a string.
Code:
let str = 'Similar Geeks';
// We split each character from the string
const str_array = str.split('')
str_array.shift()
const final = str_array.join('')
console.log(final);
Output:
imilar Geeks
Conclusion
In this tutorial we learnt different ways to remove the first character from a string using JavaScript functions. We covered the methods like substring(), replace(), slice() and shift().
Similar Posts:
How to Convert String to Lower Case using JavaScript
How to Remove Spaces From a String in JavaScript