This article mainly introduces details about how to use substr, substring, indexOf, lastIndexOf, split, and replace in js. For more information, see indexOf () returns the position of the first occurrence of a specified string value.
The lastIndexOf () method returns the last position of a specified string value and searches forward from the specified position in the string.
The substring () method is used to extract characters of a string between two specified subscripts.
Substr (start, length) indicates to extract the length string from the start position.
Split splits a string into substrings and returns the result as a string array.
Replace is used to replace or replace a substring that matches the regular expression with some characters in the string.
1. substr
Substr (start, length) indicates that the length string is truncated from the start position.
Var src = "images/off_1.png ";
Alert (src. substr (7,3 ));
Pop-up value: off
2. substring
Substring (start, end) indicates the string between start and end, including the start position but not the end position.
Var src = "images/off_1.png ";
Alert (src. substring (7,10 ));
Pop-up value: off
3. indexOF
The indexOf () method returns the position (from left to right) where a specified string value first appears in the string ). If no match exists,-1 is returned. Otherwise, the value of the string that appears for the first time is returned.
Var src = "images/off_1.png ";
Alert (src. indexOf ('t '));
Alert (src. indexOf ('I '));
Alert (src. indexOf ('g '));
The pop-up values are:-1, 0, and 3.
4. lastIndexOf
The lastIndexOf () method returns the index value (opposite to indexOf) of the first character of a character or string that appears from the right to the left)
Var src = "images/off_1.png ";
Alert (src. lastIndexOf ('/'));
Alert (src. lastIndexOf ('g '));
Pop-up values: 6, 15
5. split
Splits a string into substrings and returns the result as a string array.
Returns a string separated by spaces.
Function SplitDemo () {var s, ss; var s = "The rain in Spain falls mainly in the plain."; // separate each space character. Ss = s. split (""); return (ss );}
6. replace:
It is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
Syntax: stringObject. replace (regexp, replacement );
Parameters:
Regexp: Required. RegExp object of the pattern to be replaced.
Replacement: Required. replace text or generate a function to replace text.
Return value:
A new string, replacement is used to replace the first match of regexp or all matching results.
Note:
The replace () method of the string stringObject performs the search and replace operation. It searches stringObject for substrings that match regexp and replaces them with replacement. If regexp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.
The above section describes how to use substr, substring, indexOf, lastIndexOf, split, and replace in js.