Determine if a string contains a character? How to intercept a certain character?

Source: Internet
Author: User

The most string manipulation encountered in the write front-end process is to determine if a string contains a character, and to intercept a string.

String operation there are many ways, in fact, as long as the master above two is enough, other methods with him, good, the following two ways to explain the above.

1, judge whether a string contains a certain character, using the indexof () method;

Str.indexof ("Parameter 1", "Parameter 2"), parameter 1 indicates whether a small string is included, the parameter 2 indicates the starting position from left to right, and the default starts at 0, and Str represents the original string to look for, PS: If the original string contains more than one judgment string, only the first position is returned.

For example, to determine whether the image name "Title_clicked.png" contains the string "clicked", the code is as follows

var imageName = "Title_click.png"; alert (Imagename.indexof ("clicked"));

This will pop up 6, indicating that the click string is found in the 6th place in the subscript order.

Returns "1" if not included;

2, the interception of a string, the use of substring () method;

SUBSTRING ("Parameter 1", "Parameter 2"), parameter 1 is the starting position of the Intercept, the parameter 2 means the end position of the Intercept, PS: The interception principle is "Gu Tou disregard tail", meaning that the truncated string contains the starting position character, does not contain the end position character. such as "123". substring (0,2); The returned result is "12";

For example: Operation "Title_clicked.png" Remove the name "_clicked", the method is as follows:

var imageName = "Title_clicked.png"; var index1 = Imagename.indexof ("_clicked"); var index2 = Imagename.indexof (".") ); var newName = Imagename.substr (0,index1) + imagename.substr (INDEX2);

At this time the return of the NewName is "title.png";

Finish!

Determine if a string contains a character? How to intercept a certain character?

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.