|
| JAVASCRIPT indexOf() METHOD
The indexOf() method returns the position of the first occurrence of a specified search string or search character to look for in a string. The indexOf() method is case sensitive.
The indexOf() method takes the string or character to search for as a compulsory argument and an optional to start from argument. Always remember that the first position always starts at 0 (and not 1). The indexOf() method returns a -1 if no occurrences are found.
The general format is:
string.indexOf(tosearchfor, tostartfrom[optional]);
Example Without The Optional tostartfrom Argument:
<script type="text/javascript">
var string="The brown box";
document.write(string.indexOf("b"));
</script>
The above will output:
Example With The Optional tostartfrom Argument:
<script type="text/javascript">
var string="The brown box";
document.write(string.indexOf("b", 5));
</script>
The above will output:
Example With No Match:
<script type="text/javascript">
var string="The brown box";
document.write(string.indexOf("f"));
</script>
The above will output:
See Also:
|
|