1. Use slice() to intercept
1. Function description
The slice() method extracts a part of the string by specifying the start and end positions, and returns the extracted part with a new string. The grammar is as follows:
stringObject.slice(start, end)
Description of parameters:
- Start (required): specify where to start selecting. If it is a negative number, it specifies the position from the end of the string. That is to say, - 1 refers to the last character, - 2 refers to the penultimate character, and so on.
- End (optional): specifies where to end the selection, i.e. the character subscript at the end. If this parameter is not specified, the intercepted string contains all the characters from start to end. If this parameter is negative, it specifies characters that start at the end of the array.
2. Use examples
var str = "0123456789"; console.log("The original string:", str); console.log("From the character with index 3 to the end:", str.slice(3)); //3456789 console.log("From the penultimate character to the end:", str.slice(-3)); //789 console.log("From the beginning to the first character of index 5:", str.slice(0,5)); //01234 console.log("From the beginning to the first character of the penultimate third character:", str.slice(0,-3)); //0123456 console.log("From the character with index 3 to the first character with index 5:", str.slice(3,5)); //34 console.log("From the character with index 3 to the first character with reciprocal third character:", str.slice(3,-3)); //3456
2. Use substring() to intercept
1. Function description
(1) The substring method is used to extract characters between two specified subscripts in a string. The grammar is as follows:
stringObject.substring(start, stop)
Description of parameters:
This method returns a new string whose value contains a substring of stringObject whose content is all the characters from start to stop-1 and whose length is stop minus start.
- start (required): A non-negative integer specifying the location of the first character of the substring to be extracted in stringObject.
- stop (optional): A non-negative integer, 1 more than the last character of the substring to be extracted in the stringObject.
This method returns a new string whose value contains a substring of stringObject whose content is all the characters from start to stop-1 and whose length is stop minus start.
(2) Notes:
- If the start is equal to the end, then the method returns an empty string (that is, a string of length 0).
- If the start is larger than the end, the method exchanges the two parameters before extracting the substring.
- If the start or end is negative, it will be replaced by 0.
2. Use examples
var str = "0123456789"; console.log("The original string:", str); console.log("From the character with index 3 to the end:", str.substring(3)); //3456789 console.log("From the character with index 20 to the end:", str.substring(20)); // console.log("From the character with index 3 to the end of the first character with index 5:", str.substring(3,5)); //34 console.log("start than end The conference will be exchanged automatically. The results are as follows:", str.substring(5,3)); //34 console.log("From the character with index 3 to the end of the first character with index 20:", str.substring(3,20)); //3456789
3. Use substr() to intercept
1. Function description
The substr method is used to return a substring of the specified length starting at the specified position. The grammar is as follows:
stringObject.substr(start, length)
start (required): The starting position of the required substring. The index of the first character in the string is 0.
length (optional): the number of characters to be included in the returned substring.
length (optional): the number of characters to be included in the returned substring.
(2) Notes:
- If length is 0 or negative, an empty string is returned.
- If no length is specified, the substring will continue to the end of the stringObject.
- If the start or length is negative, it will be replaced by 0.
2. Use examples
var str = "0123456789"; console.log("The original string:", str); console.log("From the character with index 3 to the end:", str.substr(3)); //3456789 console.log("From the character with index 20 to the end:", str.substr(20)); // console.log("Intercept a 5-length string from a character with index 3:", str.substr(3,5)); //34567 console.log("A 20-length string is intercepted from a character with index 3:", str.substr(3,20)); //3456789
Appendix: Other commonly used methods
The following functions can help us to intercept strings.
1,indexOf()
Returns the subscript of the first character in the string that matches the substring.
var str = "JavaScript"; var i1 = str.indexOf("a"); //1 var i2 = str.indexOf("S"); //4 var i3 = str.indexOf("Script"); //4 var i4 = str.indexOf("k"); //-1 console.log("The original string:", str); console.log("a Index:", i1); console.log("S Index:",i2); console.log("Script Index:",i3); console.log("k Index:",i4);
2,lastIndexOf()
This method returns the first character index value of a character or string that appears from right to left (as opposed to indexOf)
var str = "JavaScript"; var i1 = str.lastIndexOf("a"); //3 var i2 = str.lastIndexOf("S"); //4 var i3 = str.lastIndexOf("Script"); //4 var i4 = str.lastIndexOf("k"); //-1
Reprinted from the original link: http://www.hangge.com/blog/cache/detail_1887.html