This article mainly summarizes the usage of substr, substring, indexOf, and lastIndexOf in js. If you need it, you can refer to it for help.
Usage of substr, substring, indexOf, and lastIndexOf in js
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