asp.net.ph

Array.IndexOf Method

System Namespace   Array Class


Returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array.

Overload List

1. Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.

2. Searches for the specified object and returns the index of the first occurrence within the section of the one-dimensional Array that extends from the specified index to the last element.

3. Searches for the specified object and returns the index of the first occurrence within the section of the one-dimensional Array that starts at the specified index and contains the specified number of elements.


Example

The following code example shows how to determine the index of the first occurrence of a specified element.

NOTE: This example shows how to use one of the overloaded versions of IndexOf. For other examples that might be available, see the individual overload topics.

[ VB ] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main ( ) 
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance ( GetType ( String ) , 12 ) 
        myArray.SetValue ( "the", 0 ) 
        myArray.SetValue ( "quick", 1 ) 
        myArray.SetValue ( "brown", 2 ) 
        myArray.SetValue ( "fox", 3 ) 
        myArray.SetValue ( "jumped", 4 ) 
        myArray.SetValue ( "over", 5 ) 
        myArray.SetValue ( "the", 6 ) 
        myArray.SetValue ( "lazy", 7 ) 
        myArray.SetValue ( "dog", 8 ) 
        myArray.SetValue ( "in", 9 ) 
        myArray.SetValue ( "the", 10 ) 
        myArray.SetValue ( "barn", 11 ) 
        
        ' Displays the values of the Array.
        Response.WriteLine ( "The Array contains the following values:" ) 
        PrintIndexAndValues ( myArray ) 
        
        ' Searches for the first occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.IndexOf ( myArray, myString ) 
        Response.WriteLine ( "The first occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex ) 
        
        ' Searches for the first occurrence of the duplicated value in the last
        ' section of the Array.
        myIndex = Array.IndexOf ( myArray, myString, 4 ) 
        Response.WriteLine ( "The first occurrence of ""{0}"" between index 4 " _
           + "and the end is at index {1}.", myString, myIndex ) 
        
        ' Searches for the first occurrence of the duplicated value in a section
        ' of the Array.
        myIndex = Array.IndexOf ( myArray, myString, 6, 5 ) 
        Response.WriteLine ( "The first occurrence of ""{0}"" between index 6 " _
           + "and index 10 is at index {1}.", myString, myIndex ) 
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues ( myArray As Array ) 
        Dim i As Integer
        For i = myArray.GetLowerBound ( 0 ) To myArray.GetUpperBound ( 0 ) 
            Response.WriteLine ( ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue ( i ) ) 
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array contains the following values:
'     [0]:    the
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumped
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
'     [9]:    in
'     [10]:    the
'     [11]:    barn
' The first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 6 and index 10 is at index 6. 

[ C# ] 
using System;
public class SamplesArray  {

   public static void Main ( )  {

      // Creates and initializes a new Array with three elements of the same value.
      Array myArray=Array.CreateInstance ( typeof ( String ) , 12 );
      myArray.SetValue ( "the", 0 );
      myArray.SetValue ( "quick", 1 );
      myArray.SetValue ( "brown", 2 );
      myArray.SetValue ( "fox", 3 );
      myArray.SetValue ( "jumped", 4 );
      myArray.SetValue ( "over", 5 );
      myArray.SetValue ( "the", 6 );
      myArray.SetValue ( "lazy", 7 );
      myArray.SetValue ( "dog", 8 );
      myArray.SetValue ( "in", 9 );
      myArray.SetValue ( "the", 10 );
      myArray.SetValue ( "barn", 11 );

      // Displays the values of the Array.
      Response.WriteLine ( "The Array contains the following values:" );
      PrintIndexAndValues ( myArray );

      // Searches for the first occurrence of the duplicated value.
      String myString = "the";
      int myIndex = Array.IndexOf ( myArray, myString );
      Response.WriteLine ( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the first occurrence of the duplicated value in the last section of the Array.
      myIndex = Array.IndexOf ( myArray, myString, 4 );
      Response.WriteLine ( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

      // Searches for the first occurrence of the duplicated value in a section of the Array.
      myIndex = Array.IndexOf ( myArray, myString, 6, 5 );
      Response.WriteLine ( "The first occurrence of \"{0}\" between index 6 and index 10 is at index {1}.", myString, myIndex );
   }


   public static void PrintIndexAndValues ( Array myArray )  {
      for ( int i = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
         Response.WriteLine ( "\t[{0}]:\t{1}", i, myArray.GetValue ( i ) );
   }
}
/*
This code produces the following output.

The Array contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumped
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn
The first occurrence of "the" is at index 0.
The first occurrence of "the" between index 4 and the end is at index 6.
The first occurrence of "the" between index 6 and index 10 is at index 6.
*/

[JScript ] 
import System;

// Creates and initializes a new Array with three elements of the same value.
var myArray : System.Array = System.Array.CreateInstance ( System.String, 12 );
myArray.SetValue ( "the", 0 );
myArray.SetValue ( "quick", 1 );
myArray.SetValue ( "brown", 2 );
myArray.SetValue ( "fox", 3 );
myArray.SetValue ( "jumped", 4 );
myArray.SetValue ( "over", 5 );
myArray.SetValue ( "the", 6 );
myArray.SetValue ( "lazy", 7 );
myArray.SetValue ( "dog", 8 );
myArray.SetValue ( "in", 9 );
myArray.SetValue ( "the", 10 );
myArray.SetValue ( "barn", 11 );

// Displays the values of the Array.
Response.WriteLine ( "The Array contains the following values:" );
PrintIndexAndValues ( myArray );

// Searches for the first occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = System.Array.IndexOf ( myArray, myString );
Response.WriteLine ( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the first occurrence of the duplicated value in the last section of the Array.
myIndex = System.Array.IndexOf ( myArray, myString, 4 );
Response.WriteLine ( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

// Searches for the first occurrence of the duplicated value in a section of the Array.
myIndex = System.Array.IndexOf ( myArray, myString, 6, 5 );
Response.WriteLine ( "The first occurrence of \"{0}\" between index 6 and index 10 is at index {1}.", myString, myIndex );
 
 
function PrintIndexAndValues ( myArray : System.Array  )  {
   for ( var i : int = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
      Response.WriteLine ( "\t[{0}]:\t{1}", i, myArray.GetValue ( i ) );
}
 
 /*
 This code produces the following output.
 
 The Array contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 The first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 10 is at index 6.
 */
See Also

Array Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph