10

I see this article but it's specific to deleting a character if it's a certain number (0, in that case).

I want to remove the first character from a string no matter what it is.

splice() and shift() won't work because they're specific to arrays:

let string = "stake";
string.splice(0, 1);
console.log(string);

let string = "stake";
string.shift();
console.log(string);

slice() gets the first character but it doesn't remove it from the original string.

let string = "stake";
string.slice(0, 2);
console.log(string);

Is there any other method out there that will remove the first element from a string?

HappyHands31
  • 4,001
  • 17
  • 59
  • 109
  • 2
    In JavaScript, strings are immutable so you cannot remove the first character. You can however make a new string without the first character, as [abney317's answer](https://stackoverflow.com/a/56468663/1115360) shows. – Andrew Morton Jun 05 '19 at 22:08

1 Answers1

24

Use substring

let str = "stake";
str = str.substring(1);
console.log(str);
abney317
  • 7,760
  • 6
  • 32
  • 56