Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 17:06:52
Original
1241 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!