Method for Determining string inclusion in JS _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to judge string inclusion in javascript. It can effectively detect whether a string contains fixed characters or substrings. It involves the use of indexOf in javascript and is very simple and practical, for more information about how to use JavaScript to determine string inclusion, see the example in this article. Share it with you for your reference. The details are as follows:

1. Example:

Var tempStr = "tempText"; var bool = tempStr. indexOf ("Texxt"); // returns an integer greater than or equal to 0. If "Text" is not included, "-1 is returned. If (bool> 0) {document. write ("contains string");} else {document. write ("does not contain string ");}

2. indexOf usage:

strObj.indexOf(subString[, startIndex])

The indexOf Function Method in JavaScript returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned. If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.

Parameters:

StrObj: required, String object or text.
SubString: required. The subString to be searched in the String object.
StarIndex: Optional. This integer indicates the index in the String object. If omitted, it will be searched from the beginning of the string;

If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.

3. Differences from lastIndexOf:

The lastIndexOf () method is used to retrieve substrings from the end of a string.

I hope this article will help you design javascript programs.

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.