js intercepts the last character of a string

Common methods for JS string interception

Several common methods for string interception:

start: Required. Non-negative integer, (index of first element is 0).

stop: Optional. A non-negative integer, 1 more than the position in stringObject of the last character of the substring to be extracted.

If it is not passed, then return to the end of the string.


str.substring(1,5); // ‘ello’

Intercepts the string from the 2nd character up to and including the 6th character (including the 2nd character, excluding the 6th character) a total of four characters


Actually it’s substring(2,0), which converts any negative number to 0. Substring always takes the smaller number as the start

Equivalent to substring(0,2). Intercept from the first digit to the third digit (including the first digit, excluding the third digit)


If only one parameter is passed, then intercept to the end of the string, starting at the passed digit position

start: start position , (the first element is indexed at 0).

length: the length to be intercepted


Intercepts a string of length 5 starting at bit 2


Intercepts two digits backward from the penultimate digit


The second argument is not passed to measure the interception to the end of the string

start: integer specifying where to start the selection ( The first element is indexed at 0).

end: integer specifying where to end the selection


Intercepts from the second position to the third position Total of two characters Excludes the fourth position of the end


This is the first position of the selection. yelloword

The second argument is not passed then intercepts to the last bit of the character,

str.slice(-1); //d

Passing a negative number intercepts the character from the end of the character

js intercepts all strings after / preceded by the specified character

letstr=’Zhang San>Li Si>Wang Wu’

Intercepts the last > preceded by the string

letindex=str. lastIndexOf(“>”)


console.log(str)//Zhang San>Li Si

Intercepts the string that comes after the last >



console.log(str)//Wang Wu

js string get last element

The substring returned by the substring method includes the character at start, but does not include the character at end.

If start and end are equal, then the method returns an empty string (that is, a string of length 0).

If start is greater than end, then the method swaps the two arguments before extracting the substring.

If start or end is negative, then it is replaced with 0.

a.substring(a.length-1) truncates the last one, going to determine

a. lastindexof(‘last word’) This is that word The last occurrence of the position, to determine whether it is equal to the length of the string

How js intercepts the last comma of a string

The strrchr() function finds the position of the last occurrence of a string in another string and returns all the characters from that position to the end of the string.