In this article, we will delete the first character of a string in Javascript. There are many ways to delete the first character of a string in JavaScript, some of them are discussed below:
Method 1: Using slice() Method: The slice() method extracts the part of a string and returns the extracted part in a new string. If we want to remove the first character of a string then it can be done by specifying the start index from which the string needs to be extracted. We can also slice the last element.
Syntax:
string.slice(startingindex, endingindex)
Example: This example shows the above-explained approach.
html
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >
Delete first character of a string in JavaScript
</ title >
</ head >
< body >
< h3 > Delete first character
of a string
</ h3 >
< p >
Click on button to display the
extracted part of the string
</ p >
< button onclick = "myGeeks()" >
Click Here!
</ button >
< p id = "GFG" ></ p >
< script >
function myGeeks() {
var str = "GeeksforGeeks";
var result = str.slice(1);
document.getElementById("GFG").innerHTML
= result;
}
</ script >
</ body >
</ html >
|
Output: here with the help of the slice method we successfully removed the first character of our string and our string started with index 1.
Method 2: Using substr() Method: The string.substr() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing starts from zero (0).
Syntax:
string.substr(Startindex, Endindex)
Example: This example shows the above-explained approach.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >
Delete first character of a string in JavaScript
</ title >
</ head >
< body >
< h3 >
Delete first character
of a string
</ h3 >
< p >
Click on button to display the
extracted part of the string
</ p >
< button onclick = "myGeeks()" >
Click Here!
</ button >
< p id = "GFG" ></ p >
< script >
function myGeeks() {
var str = "GeeksforGeeks";
var result = str.substr(1);
document.getElementById("GFG").innerHTML
= result;
}
</ script >
</ body >
</ html >
|
Output: Here substr() method return a new string that start with index 1.
Method 3: Using replace() method: The string.replace() function is an inbuilt function in JavaScript that is used to replace a part of the given string with some other string or a regular expression. The original string will remain unchanged.
Syntax:
string.replace(searchvalue, newvalue)
Example: This example shows the above-explained approach.
html
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >
Delete first character of a string in JavaScript
</ title >
</ head >
< body >
< h3 >
Delete first character
of a string
</ h3 >
< p >
Click on button to display the
extracted part of the string
</ p >
< button onclick = "myGeeks()" >
Click Here!
</ button >
< p id = "GFG" >GeeksforGeeks</ p >
< script >
function myGeeks() {
var str = document.getElementById("GFG").innerHTML;
var res = str.replace("GeeksforGeeks", "eeksforGeeks");
document.getElementById("GFG").innerHTML = res;
}
</ script >
</ body >
</ html >
|
Output: Here replace() method is used to a regular expression to match the first character and then replace it with an empty string.
Method 4: Using the substring() method: In javascript the substring method returns a substring of string based on the index numbers provided as arguments.
Syntax:
str.substring(index)
Example: In this example, we will delete the first character of the string by providing an index value.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< title >
Delete first character of a string in JavaScript
</ title >
</ head >
< body >
< h3 >
Delete first character
of a string
</ h3 >
< p >
Click on button to display the
extracted part of the string
</ p >
< button onclick = "myGeeks()" >
Click Here!
</ button >
< p id = "GFG" ></ p >
< script >
function myGeeks() {
var str = "GeeksforGeeks";
var result = str.substring(1);
document.getElementById("GFG").innerHTML
= result;
}
</ script >
</ body >
</ html >
|
Output: Here substring() method return a new string that starts with index 1.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Nov, 2023
Like Article
Save Article