16

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?

1
  • 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 shows. Commented Jun 5, 2019 at 22:08

1 Answer 1

35

Use substring

let str = "stake";
str = str.substring(1);
console.log(str);

Sign up to request clarification or add additional context in comments.

1 Comment

in addition, you can get a range of string: to remove last char: test.substr(1,str.length()-2)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.