The usage of substr,substring,indexof,lastindexof in JS __js

Source: Internet
Author: User

The usage of substr,substring,indexof,lastindexof in JS

1.substr

SUBSTR (start,length) indicates that the length of a string is intercepted from the start position.

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

The pop-up value is: off

2.substring

SUBSTRING (start,end) represents a string from start to end, including characters from the start position but not the end position.

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

The pop-up value is: off

3.indexOF

The IndexOf () method returns the first occurrence of a specified string value in a string, from left to right. If no match is returned-1, otherwise returns the subscript value of the string for the first occurrence of the position.

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

The LastIndexOf () method returns the first character index value (contrary to indexof) of a character or string appearing from right to left, noting that the index is still computed from left to right

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

The pop-up values, in turn, are: 6,15

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.