substr (Start[,length]) indicates that the length string starts at the start position
Example
The code is as follows |
Copy Code |
var str = " 0123456789 "; Alert (str.substr (0)),---------------"0123456789" Alert (STR.SUBSTR (5)),---------------"56789" Alert ( STR.SUBSTR)--------------"" 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 (0,12)),------------"0123456789" Alert (Str.substr (2,0)),-------------"" Alert ( Str.substr (2,2)),-------------"a" Alert (str.substr (2,5)),-------------"23456" Alert (Str.substr (2,12));- -----------"23456789" Alert (Str.substr (2,-2)),------------"" Alert (Str.substr ( -1,5)),------------"01234" Alert (STR.SUBSTR ( -1,-5));-----------" |
SUBSTRING (start,end) represents a string from start to end, including characters at the start position but excluding end positions
Example
The code is as follows |
Copy Code |
var str = " 0123456789 "; Alert (str.substring (0)),------------"0123456789" Alert (str.substring (5)),------------"56789" Alert ( str.substring)-----------"" 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 (0,12)),---------"0123456789" Alert (str.substring (2,0)),----------"a" alert ( Str.substring (2,2)),----------"" Alert (str.substring (2,5)),----------"234" Alert (str.substring); -------"23456789" Alert (2,-2),---------"str.substring" Alert (str.substring ( -1,5)),---------"01234" Alert (str.substring ( -1,-5));--------" |
Slice (), the first parameter represents the start position, the second parameter represents the next position in the end position, the length of the truncated string is the difference between the second parameter and the first parameter, and if the parameter value is negative, the value is added to the string length and then positive; Returns an empty string if the first argument equals greater than the second argument
In the example above we can see that the use of slice () and substring () is the same, and the return value is the same, but when the argument is negative, their return value is not the same, look at the following example
The code is as follows |
Copy Code |
Alert (Stmp.slice (2,-5));//return "I" Alert (stmp.substring (2,-5));//return "RC"
|
From the above two examples you can see that slice (2,-5) is actually slice (2,3), minus 5 plus string length 8 to positive 3 (if the first digit equals or is greater than the second digit, an empty string is returned), and substring (2,-5) is actually substring (2,0), negative numbers convert to 0,substring always take a smaller number as the starting position.
function : indexOf ()
Function: Returns the subscript of the first character in a string that matches a substring
The code is as follows |
Copy Code |
var mystring= "JavaScript"; www.111cn.net var w=mystring.indexof ("V"); W'll be 2 var x=mystring.indexof ("S"); X would be 4 var y=mystring.indexof ("Script"); y'll also be 4 var z=mystring.indexof ("key"); Z'll be-1 |
Function: Split method
The split method has two parameters and the first is a separator. That is, you can divide strings by what, for example, by commas "," and so on; the second argument is optional, controlling the number of split segments.
Split (separator, keep segment number)
The code is as follows |
Copy Code |
JavaScript code <script type= "Text/javascript" > var str = "1234"; var str1 = "Basketball, volleyball, table tennis"; var arr = str.split ("");//All Split var arr1 = Str1.split (",");//split by comma var arr2 = Str1.split (",", 2);//divide by comma, keep two paragraphs </script> |
First, Str keeps the string stored