JavaScript judgment character first appears in string position

Source: Internet
Author: User

Grammar
Stringobject.indexof (Searchvalue,fromindex)

<script type= "text/web Effects" >
var str= "Hello world!"//w lowercase
document.write (Str.indexof ("H") + "<br/>")//0
document.write (Str.indexof ("World") + "<br/>")//-1
document.write (Str.indexof ("World"))//6
</script>

Description
This method retrieves the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to start retrieving is at the fromindex of the string or at the beginning of the string (no fromindex is specified). If a searchvalue is found, it returns the position of the first occurrence of searchvalue. The character position in the Stringobject is starting at 0.

Example
The following example illustrates the use of the IndexOf method.

function Indexdemo (str2) {
var str1 = "Babebibobubabebibobu"
var s = str1.indexof (STR2);
return (s);
}

Tips and comments
Note: the IndexOf () method is case sensitive!

Note: If the string value to retrieve does not appear, the method returns-1.

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.