You can remove the first character of a string using substring:
var s1 = "foobar";
var s2 = s1.substring(1);
alert(s2); // shows "oobar"
To remove all 0's at the start of the string:
var s = "0000test";
while(s.charAt(0) === '0')
{
s = s.substring(1);
}
Answer from Shaded on Stack OverflowYou can remove the first character of a string using substring:
var s1 = "foobar";
var s2 = s1.substring(1);
alert(s2); // shows "oobar"
To remove all 0's at the start of the string:
var s = "0000test";
while(s.charAt(0) === '0')
{
s = s.substring(1);
}
Very readable code is to use .substring() with a start set to index of the second character (1) (first character has index 0). Second parameter of the .substring() method is actually optional, so you don't even need to call .length()...
TL;DR : Remove first character from the string:
str = str.substring(1);
...yes it is that simple...
Removing some particular character(s):
As @Shaded suggested, just loop this while first character of your string is the "unwanted" character...
var yourString = "0000test";
var unwantedCharacter = "0";
//there is really no need for === check, since we use String's charAt()
while( yourString.charAt(0) == unwantedCharacter ) yourString = yourString.substring(1);
//yourString now contains "test"
.slice() vs .substring() vs .substr()
.substr()EDIT: substr() is not standardized and should not be used for new JS codes, you may be inclined to use it because of the naming similarity with other languages, e.g. PHP, but even in PHP you should probably use mb_substr() to be safe in modern world :)
Quote from (and more on that in) What is the difference between String.slice and String.substring?
He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn´t.
How to remove first and last character of a string?
jquery - the best way to remove the first char of a given string in javascript - Stack Overflow
Remove the first character from a string
Remove first or last element of a string
You would actually be fine doing:
"aamerica".substring(1);
EXAMPLE
substring's second parameter is optional:
string.substring(indexA[, indexB])
"aamerica".substring(1);
your "aamerica".length; is unecessary
quote from doc:
string.substring(indexA[, indexB])
If indexB is omitted, substring extracts characters to the end of the string.
You are in total 3 options:
"aamerica".substring(1);"aamerica".slice(1);"aamerica".substr(1);
All three methods have equivalent performance.