


Summary of usage of substr, substring, indexOf, lastIndexOf in js_javascript skills
May 16, 2016 pm 05:06 PMUsage 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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string

PHP returns the ASCII value of the first character of the string

PHP returns the string from the start position to the end position of a string in another string

How to get a substring using String.substring() method in Java?

What is the use of indexof method in java

Understand the substr() function in PHP to intercept strings

What are the differences between indexof and includes?

Find the index of an element in Java using indexOf() method of ArrayList class
