These two methods are quite interesting, but the result is a 108,000 difference. I started from misuse of them and slowly entered their world. It is a coincidence that when start is 0, the two results are the same, and I have read the substr method before. Therefore, I had the illusion that only substr and no substring are available. When I find there are two ways, I will giggle at my ignorance. The following two methods are described:
Substr (start [, length]) indicates that length strings are obtained from the start position.
Substring (START, end) indicates a string from start to end, including the start position but not the end position.
The substring of JS and the substring of C # are used to extract a substring from a string, but their usage is quite different. Let's take a look at the following comparison:
Substring of JS
Syntax:
ProgramCode
String. substring (START, end)
Note:
Returns a substring from start to end (excluding end.
Example:
Program code
VaR STR = "abcdefgh ";
Document. Write (Str. substring (0, 1); // return:
Document. Write (Str. substring (2, 5); // return: CDE
Document. Write (Str. substring (7,8); // return: H
Substring of C #
Syntax:
Program code
String. substring (INT startindex)
String. substring (INT startindex, int length)
Note:
Returns a substring starting from startindex to ending, or a substring starting from startindex with a length of length.
Example:
Program code
String STR = "abcdefgh ";
Response. Write (Str. substring (0, 1); // return:
Response. Write (Str. substring (2, 3); // return: CDE
Response. Write (Str. substring (7,1); // return: H
Response. Write (Str. substring (7); // return: H
Response. Write (Str. substring (10); // error: startindex cannot exceed the string length.
Response. Write (Str. substring (7,10); // error: the index and length must reference the position in the string.
After the above instructions, we should have a clear understanding of their use, but there are several points to note about the substring of JS:
1. Start is not necessarily the first parameter, and end is not necessarily the second parameter. In substring (), the start position is 1, and the end position is 3;
2. when the substring to be returned is from the starting position to the end, the end value must be greater than or equal to the length of the string, such as the STR above. substring (), if the index starts from 0, the maximum value of end is 7, but 8 is used here. Of course, the same result is returned when the number is greater than 8, this is interesting.