JS: used to determine whether a string contains the indexOf () method, jsindexof

Source: Internet
Author: User

JS: used to determine whether a string contains the indexOf () method, jsindexof

Definition and usage

The indexOf () method returns the position of the first occurrence of a specified string value.


Syntax

stringObject.indexOf(searchvalue,fromindex)
Parameters
Description
Searchvalue Required. Specifies the string value to be retrieved.
Fromindex Optional integer parameter. Specifies the start position of the string. The valid value is 0 to stringObject. length-1. If this parameter is omitted, It is retrieved from the first character of the string.

Description

The indexOf method returns an integer. The character position in the stringObject starts from 0. If no substring is found,-1 is returned.


Example:

<Script type = "text/javascript"> var str = "Hello world! "; Alert (str. indexOf (" world "); // 6if (str. indexOf (" Hello ")! =-1) {alert ("included");} else {alert ("excluded") ;}</script>



Js checks whether a string contains a string

String. indexOf ("Check string")> 0 exists, <= 0 does not exist

Js determines whether a string is contained

If (a. indexOf ('], [') <0)
Does not contain
Else
Include

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.