JavaScript indexOf () method

Source: Internet
Author: User
Tags definition

Definition and Usage

Definition and usage

The IndexOf () method returns the position of the the ' the ' of the ' the ' a specified string value in a string.

The IndexOf () method returns the position of the specified value at the first occurrence of the string

Syntax

GrammarstringObject.indexOf(searchvalue,fromindex) 

Parameter

Parameters

Description

Comments

Searchvalue

The value that you want to match

Required. Specifies a string value to search for

Required option. Specify the value you want to find a match for

Fromindex

Starting position

Optional. Specifies where to start the search

Options available. Match the start position

Tips and Notes

Attention

Note:the indexOf () method was case sensitive!

Note: the IndexOf () method is an exact match

Note:this method Returns-1 If the string value to search for never occurs.

Note: If no matching value is available, return-1 Example

Instance

In this example we'll do different searches within a "Hello world!" string:

In this case, we're going to "Hello world!" String for different lookup matches:

<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script> 

The output of the code above would be:

The result returned is:

0
-1
6 

Try-it-yourself Demos

Interactive walkthroughs

IndexOf ()

IndexOf () to search within a string.

How to find and match a string with indexof ()

Related Article

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.