Preface:
Heaven, earth, look, what is this?
Frozen chicken, blog is still being watched ~, Haha, continue to fuel ~
Tell us the difference between substr and substring in JS today
1. Definition of w3school
1) substr
Definition: The substr () method can be extracted from stringsStartA specified number of characters starting with a subscript.
Usage:Stringobject. Substr (Start,Length)
Start |
Required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on. |
Length |
Optional. The number of characters in the substring. It must be a numerical value. If this parameter is omittedStringobjectThe start position to the end of the string. |
Case:
① Substr only transmits One Parameter
1
Output:
The string is intercepted starting from 3rd bits.
② Substr Two Parameters
1
Output:
String truncation starts from 3rd bits and starts to count 7
2) substring
Definition: The substring () method is used to extract characters of a string between two specified subscripts.
Usage:Stringobject. Substring (Start,Stop)
|
|
Start |
Required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringobject. |
Stop |
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringobject. If this parameter is omitted, the returned substring ends at the end of the string. |
Case:
① Substring only transmits One Parameter
1
Output:
The string is intercepted starting from 3rd bits.
② Substring Two Parameters
1
Output:
The string is truncated starting from 3rd bits and the coordinate is 7 characters.
2. Comparison
Similarities: both functions are the same when passing a parameter.
Differences:
Substr: the position where the negative number starts from the end of the string.
Two parameters, the last one being several lengths from the truncated position, Length
Substring: two parameters. The last one is from the captured position to the coordinate, stop
One length and one stop
This is easy to understand, hahaha,
Finally, I wish you a happy 1024 day. I would like to celebrate overtime ~~
Differences between substr and substring