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

May 16, 2016 pm 05:06 PM
indexof lastindexof substr substring

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string Jul 24, 2023 pm 12:13 PM

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 ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

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 PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

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? How to get a substring using String.substring() method in Java? Nov 18, 2023 am 08:07 AM

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

What is the use of indexof method in java What is the use of indexof method in java May 17, 2023 pm 02:28 PM

What is the use of indexof method in java

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP to intercept strings

What are the differences between indexof and includes? What are the differences between indexof and includes? Nov 24, 2023 pm 01:20 PM

What are the differences between indexof and includes?

Find the index of an element in Java using indexOf() method of ArrayList class Find the index of an element in Java using indexOf() method of ArrayList class Jul 25, 2023 pm 08:30 PM

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

See all articles