Using substring ()
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.
1.substr (start, length), length is intercepted from start.
The code is as follows |
Copy Code |
var str = ' It blog is a blog focused on it programming! ' Alert (STR.SUBSTR (0, 5));//it Blog is |
2.substring (start, end);
Intercepts from start to end (does not contain an end).
The code is as follows |
Copy Code |
var str = ' It blog is a blog focused on it programming! ' Alert (str.substring (0, 5));//it Blog is
|
Cases
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));--------" |
Use split or slice ()
Function: Split ()
Function: To store a string partition into an array using a specified delimiter
Example:
The code is as follows |
Copy Code |
Str= "Jpg|bmp|gif|ico|png"; Arr=thestring.split ("|"); Arr is an array that contains the character values "JPG", "BMP", "GIF", "ico", and "PNG" |
Function: John ()
Function: Merges an array into a string using the separator of your choice
Example:
The code is as follows |
Copy Code |
var delimitedstring=myarray.join (delimiter); var mylist=new Array ("JPG", "BMP", "GIF", "ico", "PNG"); var portablelist=mylist.join ("|"); The result is jpg|bmp|gif|ico|png. |
Function: substring ()
Function: string interception, for example, to get "Minidx" from "minidxsearchengine" to use SUBSTRING (0,6)
Function: IndexOf ()
Function: Returns the subscript of the first character in a string that matches a substring
code is as follows |
copy code |
var Mystring= "JavaScript"; var w=mystring.indexof ("V"); W'll be 2 var x=mystring.indexof ("S"); x'll be is 4 var y=mystring.indexof ("Scrip T "); y would also be 4 var z=mystring.indexof (" key "); Z'll be-1 |