Javascript InStr Function

Javascript InStr Function

The string.indexOf function is used as instr function in javascript.

syntax:
object.indexOf(searchValue,[fromIndex])

When called from a String object, this method returns the index of the first occurance of the specified searchValue argument, starting from the specified fromIndex argument. If the searchValue is not found, a value of -1 is returned. Characters in the string are indexed from left to right with the first character indexed as 0 and the last as String.length-1. Note that this method is case sensitive as shown in the example below.

The following code uses the indexOf method to find the index values of three different character strings within the myString object. Note that the third example returns -1 because the case of one of the characters does not match.

Code:
myString = new String(”webdevelopers.com”)
document.writeln(myString.indexOf(”developers”))
document.writeln(”<br>” + myString.indexOf(”com”))
document.writeln(”<br>” + myString.indexOf(”Com”))

Output:
3
14
-1

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.