Example of using javascript to determine whether a string contains a specific string and indexOf

Source: Internet
Author: User
Jsjavascript checks whether a String contains a certain String. If you are interested in using the String object to find Sub-characters and indexOf, you can refer to the following to determine the specified directory to display ads.

if(location.href.indexOf("http://www.jb51.net/codes/")>-1){ alert('ok'); }
Var Cts = "bblText"; if (Cts. indexOf ("Text")> = 0) {alert ('cts contains Text string ');}

IndexOf usage:

Returns the first occurrence of a substring in a String object.

StrObj. indexOf (subString [, startIndex])

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 it is omitted, It is searched from the beginning of the string.

Description

The indexOf method 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.

Search from left to right. Otherwise, this method is the same as lastIndexOf.

Example

The following example illustrates how to use the indexOf method.

function IndexDemo(str2){ var str1 = "BABEBIBOBUBABEBIBOBU" var s = str1.indexOf(str2); return(s); }

JavaScript indexOf is case-insensitive.

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.
The indexOf function performs search from left to right. Otherwise, this method is the same as lastIndexOf.

The following example illustrates how to use the indexOf function method.

function IndexDemo(str2){ var str1 = "BABEBIBOBUBABEBIBOBU" var s = str1.indexOf(str2); return(s); }

For more articles about how to use javascript to determine whether a string contains a specific string and indexOf examples, refer to PHP!

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.