Home >Web Front-end >Front-end Q&A >What are the methods for intercepting strings in javascript?
Javascript method of intercepting strings: 1. Use the substr() method to intercept the substring according to the specified length; 2. Use the slice() method to intercept the string according to the specified starting and ending subscript positions. ;3. Use the substring() method to intercept the string according to the specified starting and ending subscript positions.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There are three ways to intercept strings in javascript:
slice(): Extract a substring
substr(): Extract a substring
substring(): Return a substring of the string
Among them, substr The () method can intercept a substring based on the specified length, while the slice() and substring() methods both intercept strings based on the specified starting and ending subscript positions. Let me give you a detailed introduction below.
Intercept a string of specified length
The substr() method can intercept a substring according to the specified length. It contains two parameters, the first parameter indicates the starting subscript of the substring to be intercepted, and the second parameter indicates the length of the interception.
Example 1
In the following example, use lastIndexOf() to get the subscript position of the last period of the string, and then intercept 4 from the position after it. characters.
var s = "http://c.biancheng.net/index.html"; var b = s.substr(s.lastIndexOf(".") + 1,4); //截取最后一个点号后4个字符 console.log(b); //返回子字符串“html”
If the second parameter is omitted, it means that all characters from the starting position to the end are intercepted. Considering that the length of the extension is not fixed, omitting the second parameter will be more flexible.
var b = s.substr(s.lastIndexOf(".") + 1);
If the first parameter is a negative value, it means that the subscript position is calculated from the end of the string, that is, -1 means the last character, -2 means the second to last character, and so on. This is useful when the left character length is not fixed.
ECMAScript This method is no longer recommended in favor of the slice() and substring() methods.
Intercept the starting and ending subscript position string
The slice() and substring() methods are based on the specified starting and ending subscripts Position to intercept the string, they can contain two parameters, the first parameter represents the starting subscript, and the second parameter represents the ending subscript.
Example 2
The following code uses the substring() method to intercept the website host name information in the URL string.
var s = "http://c.biancheng.net/index.html"; var a = s.indexOf("c"); var b = s.indexOf("/", a); var c = s.substring(a,b); var d = s.slice(a,b);
The intercepted string contains the characters specified by the first parameter. The end point is not truncated, i.e. not included in the string.
If the second parameter is omitted, it means to intercept all strings to the end.
If the first parameter value is greater than the second parameter value, the substring() method can exchange the two parameters before performing interception, and for the slice() method, is ignored as invalid and an empty string is returned.
Example 3
The following code compares the usage of substring() method and slice() method.
var s = "http://c.biancheng.net/index.html"; var a = s.indexOf("c"); var b = s.indexOf("/", a); var c = s.substring(b, a); var d = s.slice(b, a);
When the value size of the starting point and the end point cannot be determined, it is more appropriate to use the substring() method.
If the parameter value is a negative value, the slice() method can interpret the negative sign as positioning from the right, which is the same as the slice() method of Array. But the substring() method will treat it as invalid and return an empty string.
Example 4
The following code compares the usage of substring() method and slice() method.
var s = "http://c.biancheng.net/index.html"; var a = s.indexOf("c"); var b = s.indexOf("/", a); var 1 = s.length; var c = s.substring(a-1, b-1); var d = s.slice(a-1, b-1);
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What are the methods for intercepting strings in javascript?. For more information, please follow other related articles on the PHP Chinese website!