Summary of substr, substring, indexOf, and lastIndexOf in js

Source: Internet
Author: User

Usage of substr, substring, indexOf, and lastIndexOf in js

1. substr
Substr (start, length) indicates that the length string is truncated from the start position.

Var src = "images/off_1.png ";
Alert (src. substr (7,3 ));

Pop-up value: off
 

2. substring
Substring (start, end) indicates the string between start and end, including the start position but not the end position.

Var src = "images/off_1.png ";
Alert (src. substring (7,10 ));

Pop-up value: off


3. indexOF
The indexOf () method returns the position (from left to right) where a specified string value first appears in the string ). If no match exists,-1 is returned. Otherwise, the value of the string that appears for the first time 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, and 3.


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

Var src = "images/off_1.png ";
Alert (src. lastIndexOf ('/'));
Alert (src. lastIndexOf ('G '));

Pop-up values: 6, 15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.