While working in javascript, we often need to remove the first character from a string. This article will illustrate how to delete the first character of a javascript string using different methods and examples.

Table of Contents:-

Javascript remove first character from a string using substring()

Javascript’s substring() returns a subset of the string between the start and end indexes or to the end of the string. 

Code:-

let myString = "0Javascript"
let myStringFinal = ""
myStringFinal = myString.substring(1);
console.log("Previous String: "+myString )
console.log("New String: "+myStringFinal)

Output:-

Previous String: 0Javascript
New String: Javascript

Javascript remove first character from a string using slice()

The slice() method of javascript returns a copy of a part of a string as a new string. The slice() function does not modify the original string.

Code:-

let myString = "0Javascript"
let myStringFinal = ""
myStringFinal = myString.slice( 1 )
console.log("Previous String: "+myString )
console.log("New String: "+myStringFinal)

Output:-

Previous String: 0Javascript
New String: Javascript

Javascript remove first specific character from a string

To remove a specific character from a string, we need to check the first character using if() and charAt(). The function charAt() returns the single UTF-16 code unit of the offset of string.

Code:-

let myString = "*Javascript"
let myStringFinal = ""
   if(myString.charAt(0) == '*' )
    {
    myStringFinal = myString.slice( 1 )
    console.log("Previous String: "+ myString )
    console.log("New String: "+myStringFinal)
    }

Output:-

Previous String: *Javascript
New String: Javascript

Javascript remove first character from a string if it is a comma

Code:-

 let myString = ",Javascript"
    let myStringFinal = ""
    if(myString.charAt(0) == ',' )
    {
    myStringFinal = myString.slice( 1 )
    console.log("Previous String: "+ myString )
    console.log("New String: "+myStringFinal)
    }

Output:-

Previous String: ,Javascript
New String: Javascript

Javascript trip off leading 0

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

Code:-

let myString = "0Javascript"
let myStringFinal = ""
myStringFinal = myString.replace(/^0/, "")
console.log("Previous String: "+myString )
console.log("New String: "+myStringFinal)

OR

Code:-

 let myString = "0Javascript"
    let myStringFinal = ""
    if(myString.charAt(0) == '0' )
    {
    myStringFinal = myString.slice( 1 )
    console.log("Previous String: "+ myString )
    console.log("New String: "+myStringFinal)
    }

OR

Output:-

Previous String: 0Javascript
New String: Javascript

Read More:

We hope this article helped you to delete the first character from the javascript string. Good Luck !!!