Export (0) Print
Expand All

StringCollection.IndexOf Method

Searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection.

[Visual Basic]
Public Function IndexOf( _
   ByVal value As String _
) As Integer
[C#]
public int IndexOf(
 string value
);
[C++]
public: int IndexOf(
 String* value
);
[JScript]
public function IndexOf(
   value : String
) : int;

Parameters

value
The string to locate. The value can be a null reference (Nothing in Visual Basic).

Return Value

The zero-based index of the first occurrence of value in the StringCollection, if found; otherwise, -1.

Remarks

This method performs a linear search; therefore, the average execution time is proportional to Count. That is, this method is an O(n) operation, where n is Count.

This method determines equality by calling Object.Equals. String comparisons are case-sensitive.

Example

[Visual Basic, C#, C++] The following code example searches the StringCollection for an element.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Checks whether the collection contains "orange" and, if so, displays its index.
      If myCol.Contains("orange") Then
         Console.WriteLine("The collection contains ""orange"" at index {0}.", myCol.IndexOf("orange"))
      Else
         Console.WriteLine("The collection does not contain ""orange"".")
      End If 

   End Sub 'Main

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()

      While myEnumerator.MoveNext()
         Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      Console.WriteLine()

   End Sub 'PrintValues

End Class 'SamplesStringCollection 


'This code produces the following output.
'
'Initial contents of the StringCollection:
'   RED
'   orange
'   yellow
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'The collection contains "orange" at index 1.
'


[C#] 
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Checks whether the collection contains "orange" and, if so, displays its index.
      if ( myCol.Contains( "orange" ) )
         Console.WriteLine( "The collection contains \"orange\" at index {0}.", myCol.IndexOf( "orange" ) );
      else
         Console.WriteLine( "The collection does not contain \"orange\"." );

   }

   public static void PrintValues( IEnumerable myCol )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The collection contains "orange" at index 1.

*/

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintValues(IEnumerable* myCol) {
   System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"   {0}", myEnumerator->Current);
   Console::WriteLine();
}

int main() {

   // Creates and initializes a new StringCollection.
   StringCollection* myCol = new StringCollection();
   String* myArr[] = { S"RED", S"orange", S"yellow", S"RED", S"green", S"blue", S"RED", S"indigo", S"violet", S"RED" };
   myCol->AddRange(myArr);

   Console::WriteLine(S"Initial contents of the StringCollection:");
   PrintValues(myCol);

   // Checks whether the collection contains S"orange" and, if so, displays its index.
   if (myCol->Contains(S"orange"))
      Console::WriteLine(S"The collection contains \"orange\" at index {0}.", __box(myCol->IndexOf(S"orange")));
   else
      Console::WriteLine(S"The collection does not contain \"orange\".");
}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The collection contains "orange" at index 1.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

StringCollection Class | StringCollection Members | System.Collections.Specialized Namespace | Contains | Performing Culture-Insensitive String Operations

Show:
© 2015 Microsoft