Adscend

Click Here



Click Here

Tuesday

String Function IndexOf()

String Function IndexOf()

Object: String
Method or Function: indexof(String)
Syntax: String. indexof(String)

Indexof function finds the position of a character or word in a string.
Indexof function takes a character or string as an argument and searches the given string in the main string and returns the index or starting (first occurrence) position of the string.
If the Indexof function returns -1 ,it indicates that the searching string or character is not present in the main string.


Example

<script type="text/javascript">
var string="hello everybody, my name is Aspell";
document.write(string.indexOf("everybody"));
</script>


Note: ‘everybody’ starts on the 6th letter of the string and computer language start counting from zero.
If we write,
document.write(string.indexOf("Aspell"));
The browser will show the following:
28
If we write ‘joe’, that not exist in the string, browser will return a negative number.
document.write(string.indexOf("joe"));
The browser will show the following:
-1
-1 indicates that ,there is no value or name like “joe” and this is false.



No comments:

Post a Comment