var a=[1,2,3,4,5];
NSB.Print( a.indexOf(3) )
NSB.Print( "abcde".indexOf("c") )
NSB.Print( a.indexOf(9) )
No edit summary |
|||
Line 5: | Line 5: | ||
The IndexOf() method returns the position of ''item'' in ''var'', starting the search at ''fromIndex''. The variable ''item'' can be a string or an array. The result returned starts with 0. If ''item'' is not found, -1 is returned. | The IndexOf() method returns the position of ''item'' in ''var'', starting the search at ''fromIndex''. The variable ''item'' can be a string or an array. The result returned starts with 0. If ''item'' is not found, -1 is returned. | ||
== Example | == Example == | ||
Find an item: | |||
<tabber> | |||
< | JavaScript= | ||
<syntaxhighlight lang="JavaScript"> | |||
var a=[1,2,3,4,5]; | |||
NSB.Print( a.indexOf(3) ) | |||
NSB.Print( "abcde".indexOf("c") ) | |||
NSB.Print( a.indexOf(9) ) | |||
</syntaxhighlight> | |||
|-| | |||
BASIC= | |||
<syntaxhighlight lang="vb.net"> | |||
a=[1,2,3,4,5] | a=[1,2,3,4,5] | ||
Print IndexOf(a,3) | Print IndexOf(a,3) | ||
Print IndexOf("abcde","c") | Print IndexOf("abcde","c") | ||
Print IndexOf(a,9) | Print IndexOf(a,9) | ||
</syntaxhighlight> | |||
</tabber> | |||
< | |||
== Output == | |||
</ | |||
<pre> | <pre> | ||
2 | 2 |
IndexOf(var, item, fromIndex)
The IndexOf() method returns the position of item in var, starting the search at fromIndex. The variable item can be a string or an array. The result returned starts with 0. If item is not found, -1 is returned.
Find an item:
var a=[1,2,3,4,5];
NSB.Print( a.indexOf(3) )
NSB.Print( "abcde".indexOf("c") )
NSB.Print( a.indexOf(9) )
a=[1,2,3,4,5]
Print IndexOf(a,3)
Print IndexOf("abcde","c")
Print IndexOf(a,9)
2 2 -1