This article mainly introduces the Slice, Substring, and Substr functions for intercepting strings in JavaScript. For more information, see in JavaScript, the method used to extract substrings is Slice, Substring, and Substr.
The Code is as follows:
// Slice
// Syntax: string. slice (start [, stop])
"Good news, everyone! ". Slice (5, 9 );
// 'News'
// Substring
// Syntax: string. substring (indexA [, indexB])
"Good news, everyone! ". Substring (5, 9 );
// 'News'
// Substr
// Syntax: string. substr (start [, length])
"Good news, everyone! ". Substr (5, 4 );
// 'News'
Enter a start index parameter and an optional end index (or length) parameter in the three methods.
But they differ in some important aspects:
1. The substr () method extracts a specified number of characters from a specified position.
Param: start starts to extract the location index of the character, and length is used to extract the length of the number of characters.
Return: A New String. The length starting from start.
Different browsers are inconsistent. modern browsers allow the start index parameter to be a negative number to indicate the number of characters to be extracted from the end of a string. However, in ie8 and earlier versions, the start index parameter is calculated at least from 0. [Substr is the ECMAScript feature appended to the Web browser. It is not recommended that the start index be negative when used]
The Code is as follows:
Var str = "abcdefghij ";
Console. log ("(1):" + str. substr (1); // (1): bcdefghij
Console. log ("(1, 2):" + str. substr (1, 2); // (1, 2): bc
Console. log ("(-3):" + str. substr (-3); // (-3): hij
Console. log ("(-3, 2):" + str. substr (-3, 2); // (-3, 2): hi
Console. log ("(20, 2):" + str. substr (20, 2); // (20, 2 ):
Console. log ("(-20, 2):" + str. substr (-20, 2); // (-20, 2): AB
// Ie8 and earlier
Console. log ("(-3):" + str. substr (-2); // (-20, 2): hij
Console. log ("(-3, 2):" + str. substr (-2); // (-20, 2): AB
2. The substring () method is used to extract a subset from one index of a string to another, or until the end of the string.
Param: indexA, indexB two parameter values are an integer between 0 and the length of the string.
Return: returns a new string that ranges from the index to the index, including small index position characters, not large index position characters.
The substring parameter can be reversed. It always uses a small parameter value as the start and a large parameter value as the end. If the parameter is less than 0 or NaN, it is regarded as 0. If the parameter is greater than the length of the string, it is considered as the length value of the string.
The Code is as follows:
// Assumes a print function is defined
Var anyString = "Mozilla ";
// Displays "Moz"
Console. log (anyString. substring (0, 3 ));
Console. log (anyString. substring (3,0 ));
// Displays "lla"
Console. log (anyString. substring (4, 7 ));
Console. log (anyString. substring (7,4 ));
// Displays "Kill ill"
Console. log (anyString. substring (0, 6 ));
// Displays "Mozilla"
Console. log (anyString. substring (0, 7 ));
Console. log (anyString. substring (0, 10 ));
3. slice extracts part of the string.
Param: beginSlice starts to extract the index of the character location, which can be negative. If it is negative, it is regarded as (sourceLength-beginSlice), and sourceLength is the length of the string, that is: index of the position of the extracted characters starting from the end of the string. If omitted, the extraction ends. If it is a negative value, it is regarded as (sourceLength-endSlice ).
Return: returns a new string of all characters starting from start to end (excluding end.
All parameters can be negative. If the index is negative, it starts from the end of the string.
The Code is as follows:
Var str1 = "The morning is upon us .";
Console. log (str1.slice (4,-2); // morning is upon u
Var str = "The morning is upon us .";
Str. slice (-3); // "us ."
Str. slice (-3,-1); // "us"
Str. slice (0,-1); // "The morning is upon us"