Substr in js the substring indexOf lastIndexOf usage summary

  • 2020-03-30 01:06:50
  • OfStack

Substr in js, the substring, indexOf, the use of the lastIndexOf etc

1. The substr
Substr (start,length) intercepts a string of length from the start position.

Var SRC = "images/off_1. PNG";
Alert (SRC) substr (7, 3));

The pop-up value is: off

2. The substring
Substring (start,end) represents the string from start to end, including the character of the start position but not the character of the end position.

Var SRC = "images/off_1. PNG";
Alert (SRC. The substring (7, 10));

The pop-up value is: off

3. The indexOF
The indexOf() method returns the first occurrence (from left to right) of a specified string value in a string. Returns -1 if there is no match, otherwise returns the lower value of the string at the first occurrence.

Var SRC = "images/off_1. PNG";
Alert (SRC) indexOf (' t '));
Alert (SRC) indexOf (' I '));
Alert (SRC) indexOf (" g "));

The popup values are in order: -1,0,3

4. The lastIndexOf
The lastIndexOf() method returns the index value of the first character of a character or string that appears from right to left (as opposed to indexOf)

Var SRC = "images/off_1. PNG";
Alert (SRC) lastIndexOf ('/'));
Alert (SRC) lastIndexOf (" g "));

The pop-up values are, in order, 6,15

Related articles: