JavaScript indexof function with square

Source: Internet
Author: User

JavaScript indexof function with square

Definitions and usage
The IndexOf () method returns the location of the first occurrence of a specified string value in a string.

Stringobject.indexof (searchvalue,fromindex) parameter description
Searchvalue required. A string value that requires retrieval.
Fromindex an optional integer parameter. Specify where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, it is retrieved from the first character of the string.

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.
Tips and comments
Note: the IndexOf () method is case sensitive!

Note: If the string value to retrieve does not appear, the method returns-1.
In this case, we'll do a different search within the "Hello world!" string:

<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 above code:


Strobj.indexof (substring[, StartIndex])


Required option. A String object or text.


Required option. The substring to find in the string object.


Options available. An integer value that indicates the index in which to start the lookup within a String object. If omitted, it is looked up from the beginning of the string.

The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.

If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.

Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.

The following example illustrates the use of the IndexOf method.

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.