Interception of the use of strings more widely, there are many methods, this article rough collation of some, interested in the amount of friends can only refer to the next
Use substring () or slice ()
Function: Split ()
Function: Use a specified delimiter to store a string into an array
Example:
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 delimiter of your choice
Example:
Copy CodeThe code is as follows:
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, such as want to get "minidx" from "minidxsearchengine" will use SUBSTRING (0,6)
Function: IndexOf ()
Function: Returns the subscript of the first character of a matched substring in a string
Copy CodeThe code is as follows:
var mystring= "JavaScript";
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
Continued:
1.substring method
Definition and usage
The substring method is used to extract the character of a string intermediary between two specified subscripts.
Grammar
Stringobject.substring (Start,stop)
Parameter description
Start Required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string.
return value
A new string value that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.
Description
The substring returned by the substring method includes the character at start, but does not include the character at end.
If start is equal to end, then the method returns an empty string (that is, a string of length 0).
If start is larger than end, then the method swaps the two parameters before extracting the substring.
If start or end is negative, it will be replaced with 0.
2.substr method
Definition and usage
The Substr method is used to return 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.
Example:
Copy CodeThe code is as follows:
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));----------"01"
Alert (str.substring (2,2));----------""
Alert (str.substring (2,5));----------"234"
Alert (str.substring (2,12));---------"23456789"
Alert (str.substring (2,-2));---------"01"
Alert (str.substring ( -1,5));---------"01234"
Alert (str.substring ( -1,-5));--------""
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));-------------"23"
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));-----------""
A detailed collation of the common methods of JS intercept string