Let’s say you have a string, and you want to remove the first character in it.
假设您有一个字符串,并且想要删除其中的第一个字符。
How can you do so?
你该怎么做?
One easy solution is to use the
slice()
method, passing
1
as parameter:
一种简单的解决方案是使用
slice()
方法,将
1
作为参数传递:
const text = 'abcdef'
const editedText = text.slice(1) //'bcdef'
Note that the
slice()
method does not modify the original string.
请注意,
slice()
方法不会修改原始字符串。
It creates a new string, this is why I assign it to a new variable in the above example.
它创建了一个新字符串,这就是为什么在上面的示例中将其分配给新变量的原因。
翻译自:
https://flaviocopes.com/how-to-remove-first-char-string-js/