IndexOf: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "IndexOf(''var'', ''item'') == Description == The IndexOf() method returns the position of ''item'' in ''var''. ''item'' can be a string or an array. The result returned star...")
 
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
IndexOf(''var'', ''item'')
IndexOf(''var'', ''item'', ''fromIndex'')


== Description ==
== Description ==


The IndexOf() method returns the position of ''item'' in ''var''. ''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 (Basic) ==
== Example ==
Find an item:


Add an item to the end:
<tabber>
<pre>
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)
</pre>
The result will be:
<pre>
2
2
-1
</pre>


== Example (JavaScript) ==
</syntaxhighlight>
Add an item to the end.
</tabber>
<pre>
 
var a=[1,2,3,4,5];
== Output ==
NSB.Print( a.indexOf(3) )
NSB.Print( "abcde".indexOf("c") )
NSB.Print( a.indexOf(9) )
</pre>
The result will be:
<pre>
<pre>
2
2
Line 38: Line 36:
== Related Items ==
== Related Items ==


[[array|Array]], [[dim|Dim]], [[instr|InStr]]
[[array|Array]], [[dim|Dim]], [[Instr/InstrRev|InStr]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Variable Handling]]
[[Category:Variable Handling]]
[[Category:BASIC Functions]]

Latest revision as of 16:11, 24 July 2019

IndexOf(var, item, fromIndex)

Description

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

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)

Output

2
2
-1

Related Items

Array, Dim, InStr