转载: [How to Remove the Last Character from a String in JavaScript - Mastering JS](https://masteringjs.io/tutorials/fundamentals/remove-last-character#:~:text=Nov 12%2C 2021-,To remove the last character from a string in JavaScript,length - 1)
To remove the last character from a string in JavaScript, you should use the slice()
method. It takes two arguments: the start index and the end index. slice()
supports negative indexing, which means that slice(0, -1)
is equivalent to slice(0, str.length - 1)
.
1 | let str = 'Masteringjs.ioF'; |
# Alternative Methods
slice()
is generally easier, however other methods available are substring()
and replace()
. substring()
does not have negative indexing, so be sure to use str.length - 1
when removing the last character from the string. replace()
takes either a string or a regular expression as its pattern
argument. Using /.$/
as the regular expression argument matches the last character of the string, so .replace(/.$/, '')
replaces the last character of the string with an empty string.
1 | let str = 'Masteringjs.ioF'; |
# Advanced Features
With replace()
, you can specify if the last character should be removed depending on what it is with a regular expression. For example, suppose you want to remove the last character only if the last character is a number. You can use .replace(/\d$/, '')
as shown below.
1 | // For a number, use \d$. |
# Comments from MDN
String.prototype.substring() - JavaScript | MDN
# The difference between substring() and substr()
There are subtle differences between the substring()
and substr()
methods, so you should be careful not to get them confused.
- The two parameters of
substr()
arestart
andlength
, while forsubstring()
, they arestart
andend
. substr()
'sstart
index will wrap to the end of the string if it is negative, whilesubstring()
will clamp it to0
.- Negative lengths in
substr()
are treated as zero, whilesubstring()
will swap the two indexes ifend
is less thanstart
.
Furthermore, substr()
is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
1 | const text = 'Mozilla'; |
# Differences between substring() and slice()
The substring()
and slice()
methods are almost identical, but there are a couple of subtle differences between the two, especially in the way negative arguments are dealt with.
The substring()
method swaps its two arguments if indexStart
is greater than indexEnd
, meaning that a string is still returned. The slice()
method returns an empty string if this is the case.
1 | const text = 'Mozilla'; |
If either or both of the arguments are negative or NaN
, the substring()
method treats them as if they were 0
.
1 | console.log(text.substring(-5, 2)); // => "Mo" |
slice()
also treats NaN
arguments as 0
, but when it is given negative values it counts backwards from the end of the string to find the indexes.
1 | console.log(text.slice(-5, 2)) // => "" |
See the slice()
page for more examples with negative numbers.
# Replacing a substring within a string
The following example replaces a substring within a string. It will replace both individual characters and substrings. The function call at the end of the example changes the string Brave New World
to Brave New Web
.
1 | // Replaces oldS with newS in the string fullS |
Note that this can result in an infinite loop if oldS
is itself a substring of newS
— for example, if you attempted to replace ‘ World
’ with ‘ OtherWorld
’ here.
A better method for replacing strings is as follows:
1 | function replaceString(oldS, newS, fullS) { |
The code above serves as an example for substring operations. If you need to replace substrings, most of the time you will want to use String.prototype.replace()
.