This article collects a variety of character interception methods, including commonly used JS intercept string, intercept string functions and commonly used interception, very simple and practical, the need for small partners can refer to.
This article summarizes three ways to intercept strings using JavaScript, and also sums up two ways to intercept Chinese strings, and share them with each other.
1.substring method
Definitions and usage
The substring method is used to extract characters from a string mediation between two specified subscripts.
Grammar
Stringobject.substring (Start,stop)
Parameter description
Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject. If this argument is omitted, the returned substring continues to the end of the string.
return value
A new string that contains a substring of stringobject whose contents are all characters from the start to the stop-1, whose length is stop minus start.
Description
The substring returned by the substring method includes the character at start, but does not include the character at the end.
If start is equal to end, then the method returns an empty string (that is, a length of 0).
If start is larger than end, the method swaps the two parameters before extracting the substring.
If start or end is a negative number, it is replaced with 0.
Specific Use cases:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 |
var str = "0123456789"; Alert (str.substring (0)),------------"0123456789" Alert (str.substring (5)),------------"56789" alert (str.substring (10)); -----------"" Alert (str.substring),-----------"" Alert (str.substring ( -5)),-----------"0123456789" alert ( Str.substring ( -10))----------"0123456789" alert (str.substring ( -12)),----------"0123456789" Alert str.substring ( 0,5))----------"01234" alert (str.substring (0,10)),---------"0123456789" alert (str.substring);---------" 0123456789 "Alert" (Str.substring (2,0)),----------"Alert" (Str.substring (2,2)),----------"" Alert str.substring ( 2,5);----------"234" alert (str.substring (2,12)),---------"23456789" alert (str.substring);---------"01" Alert (str.substring ( -1,5));---------"01234" alert (str.substring ( -1,-5));--------"" |
2.substr method
Definitions and usage
The Substr method returns a substring of the specified length starting at the specified position.
Grammar
Stringobject.substr (start [, length])
Parameter description
Start Required. The starting position of the desired substring. The index of the first character in the string is 0.
Length is optional. The number of characters that should be included in the returned substring.
Description
If length is 0 or negative, an empty string is returned.
If this argument is not specified, the substring continues to the end of the stringobject.
Specific Use cases:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 |
var str = "0123456789"; Alert (STR.SUBSTR (0)),---------------"0123456789" Alert (STR.SUBSTR (5)),---------------"56789" Alert (STR.SUBSTR (10 )--------------"" Alert (STR.SUBSTR);--------------"" Alert (Str.substr ( -5));--------------"0123456789" alert (Str.substr ( -10));-------------"0123456789" alert (STR.SUBSTR ( -12));-------------"0123456789" Alert Str.substr ( 0,5)-------------"01234" alert (STR.SUBSTR (0,10)),------------"0123456789" alert (STR.SUBSTR);------------ "0123456789" alert (STR.SUBSTR (2,0)),-------------"" Alert (Str.substr (2,2)),-------------"?" Alert (str.substr )-------------"23456" alert (2,12),------------"23456789" alert (STR.SUBSTR)------------"" Alert (STR.SUBSTR ( -1,5));------------"01234" alert (STR.SUBSTR ( -1,-5));-----------"" |
3. Custom Methods
The above two methods can only be used in English and digital interception, encountered in Chinese will be helpless, forced to use will lead to garbled situation, after my certification research finally summed up two ways to use JavaScript interception string contains Chinese methods:
The first method:
?
| 1 2 3 4 5 6 7 8 9 10 11 |
String.prototype.sub=function (n) {var r=/[^x00-xff]/g; if (This.replace (R, "MM"). length<=n) {return this;} var m= Math.floor (N/2); for (Var i=m;i<this.length;i++) {if This.substr (0,i). Replace (R, "MM"). Length>=n) {return this.substr (0,i) + "... "; } } return this; } |
The second method:
Intercept string contains Chinese processing, parameter meaning: (string, intercept length, whether increase ...)
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 |
function subString (str, len, Hasdot) {var newlength=0; var newstr= ""; var chineseregex=/[^x00-xff]/g; var singlechar= '; v Ar strlength=str.replace (Chineseregex, ' * * '). length; for (Var i=0;i < strlength;i++) {Singlechar=str.charat (i). toString (); if (Singlechar.match (Chineseregex)!= null) { newlength+=2; }else{newlength++} if (Newlength>len) {break;} Newstr+=singlechar; } if (Hasdot && strlength>len) {newstr+= ' ... ';} return newstr; } |
The above uses JS to intercept the Chinese string the concrete use method is as follows:
?
The above is all the contents of this article, I hope you can enjoy.