Home >Web Front-end >JS Tutorial >Summary of usage of substr, substring, indexOf, lastIndexOf in js_javascript skills

Summary of usage of substr, substring, indexOf, lastIndexOf in js_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:06:521313browse

Usage of substr, substring, indexOf, lastIndexOf, etc. in js

1.substr
substr(start, length) means starting from the start position, intercepting a string of length length.

var src="images/off_1.png";
alert(src.substr(7,3));

The pop-up value is: off

2.substring
substring(start,end) represents the string from start to end, including the character at the start position but excluding the character at the end position.

var src="images/off_1.png";
alert(src.substring(7,10));

The pop-up value is: off


3.indexOF
indexOf() method returns the position (from left to right) where a specified string value first appears in the string. If there is no match, -1 is returned, otherwise the subscript value of the string at which the first occurrence occurs 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,3


4.lastIndexOf
lastIndexOf() method returns the first character index value of a certain character or string from right to left (opposite to indexOf)

var src="images/off_1.png";
alert(src.lastIndexOf('/'));
alert(src.lastIndexOf('g'));

The pop-up values ​​are: 6, 15

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn