JS in the use of substr,substring,indexof,lastindexof, etc.

Source: Internet
Author: User

1.substr
SUBSTR (Start,length) is a string that starts at the start position and intercepts length lengths.

var src= "Images/off_1.png";
Alert (SRC.SUBSTR (7,3));

The popup value is: Off

2.substring
SUBSTRING (start,end) represents a string from start to end, including the character of the start position but not the end position.

var src= "Images/off_1.png";
Alert (src.substring (7,10));

The popup value is: Off


3.indexOF
The IndexOf () method returns the position (left-to-right) of the first occurrence of a specified string value in a string. Returns 1 if there is no match, otherwise returns the subscript value of the string where the first occurrence occurs.

var src= "Images/off_1.png";
Alert (Src.indexof (' t '));
Alert (Src.indexof (' I '));
Alert (Src.indexof (' G '));

The popup value is: -1,0,3


4.lastIndexOf
The LastIndexOf () method returns the first character index value of a character or string that appears right-to-left (as opposed to indexof)

var src= "Images/off_1.png";
Alert (Src.lastindexof ('/'));
Alert (Src.lastindexof (' G '));

The popup values are: 6,15

JS in the use of substr,substring,indexof,lastindexof, etc.

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.