Skip to content Skip to sidebar Skip to footer

The Best Way To Remove The First Char Of A Given String In Javascript

Using jquery, underscore or pure javascript what is the best way to remove the first char of a given string? I will make something like this: 'aamerica'.substring(1,'aamerica'.leng

Solution 1:

You would actually be fine doing:

"aamerica".substring(1);

EXAMPLE

substring's second parameter is optional:

string.substring(indexA[, indexB])

Solution 2:

"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.

Solution 3:

You can use slice: "aamerica".slice(1);

Solution 4:

You can't do it much better than you already are.

You can, however, omit the 2nd parameter when you want your substring to go to the end of the string.

var s = "aamerica".substring(1);

Post a Comment for "The Best Way To Remove The First Char Of A Given String In Javascript"