This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
collection.indexOf( )

collection.indexOf( ) 


 

collection.indexOf ( toSearch {; startFrom} ) -> Function result 
Parameter Type   Description
toSearch  Expression in Expression to search in the collection
startFrom  Longint in Index to start the search at
Function result  Longint in Index of the first occurrence of toSearch in the collection, -1 if not found

The collection.indexOf( ) method searches the toSearch expression among collection elements and returns the index of the first found occurrence, or -1 if it was not found.

Note: This method does not modify the original collection.

In toSearch, pass the expression to find in the collection. You can pass:

  • a scalar value (text, number, boolean, date),
  • the null value,
  • an object or a collection reference.

toSearch must match exactly the element to find (the same rules as for the equality operator are applied, see Comparison Operators).

Optionally, you can pass the index of collection from which to start the search in startFrom.

  • If startFrom >= the collection's length, -1 is returned, which means the collection is not searched.
  • If startFrom < 0, it is considered as the offset from the end of the collection (startFrom:=startFrom+length).
    Note: Even if startFrom is negative, the collection is still searched from left to right.
  • If startFrom = 0, the whole collection is searched (default).

Example  

 C_COLLECTION($col)
 $col:=New collection(1;2;"Henry";5;3;"Albert";6;4;"Alan";5)
 $i:=$col.indexOf(3) //$i=4
 $i:=$col.indexOf(5;5) //$i=9
 $i:=$col.indexOf("al@") //$i=5
 $i:=$col.indexOf("Hello") //$i=-1



See also 

collection.lastIndexOf( )

 
PROPERTIES 

Product: 4D
Theme: Collections
Number: 805255

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)