In this article, you’ll learn how to remove the first character from a string in Javascript. You can achieve this using a slice()
, substring()
and substr()
methods.
In this example, we used the slice()
method of javascript. The slice()
method slices the array from the start index to the end index. The end index if unspecified is taken as array length.
Here is an example to remove the first character from the string using the slice()
method in Javascript.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeCharacter(str){
let tmp = str.split('');
return tmp.slice(1).join('');
}
let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"
In this example, we used the substring()
method of javascript. The substring()
method returns a string between the start index and the end index. The end index if unspecified is assumed as string length, as in this case.
Here is an example to remove the first character from the string using the substring()
method in Javascript.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeCharacter(str){
return str.substring(1)
}
let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"
In this example, we used the substr()
method of javascript. The substr()
method returns the specified number of characters from the specified index from the given string. And the second parameter length is optional. The length is used for the number of characters to extract. If omitted, it extracts the rest of the string. If the length is 0 or negative value then it returns an empty string.
Here is an example to remove the first character from the string using the substr()
method in Javascript.
let input = "Tutorialsrack.com"
// Function to Remove Character
function removeCharacter(str){
return str.substr(1)
}
let output = removeCharacter(input);
console.log(`Output is ${output}`);
// "Output is utorialsrack.com"
I hope this article will help you to understand how to remove the first character from a string in Javascript.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments