Below are some of the functions that are frequently used to intercept strings in js. Every function provides an example here. For more information, see.
Function: indexOf ()
Function: returns the subscript of the first character matching the substring in a string.
The Code is as follows:
Var myString = "JavaScript ";
Var w = myString. indexOf ("v"); w will be 2
Var x = myString. indexOf ("S"); x will be 4
Var y = myString. indexOf ("Script"); y will also be 4
Var z = myString. indexOf ("key"); z will be-1
Function: split ()
Function: uses a specified separator to split a string and store it in an array.
The Code is as follows:
Str = "jpg | bmp | gif | ico | png ";
Arr = theString. split ("| ");
// Arr is an array containing character values "jpg", "bmp", "gif", "ico", and "png"
Function: Join ()
Function: combines an array into a string var delimitedString = myArray. join (delimiter) using the separator you selected );
The 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.
Functions: slice () and substring ()
Slice and substring can both accept one or two parameters. The 1st parameters are used to obtain the starting position of the string to be truncated, if the first parameter is not null, it is the first digit in the end position of the string to be truncated (that is, the obtained end position is not in the return value ), if it is null, It is truncated to the last character of the entire string.
The Code is as follows: