Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CompareInfo.IndexOf Method (String, Char, CompareOptions)

Searches for the specified character and returns the zero-based index of the first occurrence within the entire source string using the specified CompareOptions value.

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

public virtual int IndexOf (
	string source,
	char value,
	CompareOptions options
)
public int IndexOf (
	String source, 
	char value, 
	CompareOptions options
)
public function IndexOf (
	source : String, 
	value : char, 
	options : CompareOptions
) : int

Parameters

source

The string to search.

value

The character to locate within source.

options

The CompareOptions value that defines how the strings should be compared. options is either the value Ordinal used by itself, or the bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.

Return Value

The zero-based index of the first occurrence of value within the entire source using the specified CompareOptions value, if found; otherwise, -1.
Exception typeCondition

ArgumentNullException

source is a null reference (Nothing in Visual Basic).

ArgumentException

options contains an invalid CompareOptions value.

The source string is searched forward starting at the beginning of the string and ending at the end of the string.

The CompareOptions.StringSort value is not valid for this method.

If options does not include the Ordinal value, this overload performs a culture-sensitive search; that is, if the char is a Unicode value representing a precomposed character, such as the ligature 'Æ' (U+00C6), it might be considered equivalent to any occurrence of its components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. If options includes the Ordinal value, this overload performs an ordinal (culture-insensitive) search; that is, a char is considered equivalent to another char only if the Unicode values are the same. Overloads of String.IndexOf that search for a char perform an ordinal search, while those that search for a string perform a culture-sensitive search.

The following code example determines the indexes of the first and last occurrences of a character or a substring within a string.

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Creates CompareInfo for the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      Console.WriteLine();
      Console.WriteLine( "No options    : {0}", myStr );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE" ), myComp.LastIndexOf( myStr, "AE" ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae" ), myComp.LastIndexOf( myStr, "ae" ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ' ), myComp.LastIndexOf( myStr, 'Æ' ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ' ), myComp.LastIndexOf( myStr, 'æ' ) );
      Console.WriteLine( "Ordinal       : {0}", myStr );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", CompareOptions.Ordinal ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Æ', CompareOptions.Ordinal ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'æ', CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}", myStr );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", CompareOptions.IgnoreCase ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Æ', CompareOptions.IgnoreCase ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'æ', CompareOptions.IgnoreCase ) );

      // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
      Console.WriteLine();
      Console.WriteLine( "No options    : {0}", myStr );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308" ), myComp.LastIndexOf( myStr, "U\u0308" ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308" ), myComp.LastIndexOf( myStr, "u\u0308" ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü' ), myComp.LastIndexOf( myStr, 'Ü' ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü' ), myComp.LastIndexOf( myStr, 'ü' ) );
      Console.WriteLine( "Ordinal       : {0}", myStr );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", CompareOptions.Ordinal ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Ü', CompareOptions.Ordinal ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'ü', CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}", myStr );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", CompareOptions.IgnoreCase ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Ü', CompareOptions.IgnoreCase ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'ü', CompareOptions.IgnoreCase ) );

   }

   public static void PrintMarker( String Prefix, int First, int Last )  {

      // Determines the size of the array to create.
      int mySize;
      if ( Last > First )
         mySize = Last;
      else
         mySize = First;

      if ( mySize > -1 )  {

         // Creates an array of Char to hold the markers.
         Char[] myCharArr = new Char[mySize+1];

         // Inserts the appropriate markers.
         if ( First > -1 )
         myCharArr[First] = 'f';
         if ( Last > -1 )
            myCharArr[Last] = 'l';
         if ( First == Last )
         myCharArr[First] = 'b';

         // Displays the array of Char as a String.
         Console.WriteLine( "{0}{1}", Prefix, new String( myCharArr ) );

      }
      else
         Console.WriteLine( Prefix );

   }

}


/*
This code produces the following output.

No options    : Is AE or ae the same as Æ or æ?
           AE :    f                    l
           ae :          f                   l
            Æ :    f                    l
            æ :          f                   l
Ordinal       : Is AE or ae the same as Æ or æ?
           AE :    b
           ae :          b
            Æ :                         b
            æ :                              b
IgnoreCase    : Is AE or ae the same as Æ or æ?
           AE :    f                         l
           ae :    f                         l
            Æ :    f                         l
            æ :    f                         l

No options    : Is U" or u" the same as Ü or ü?
           U" :    f                    l
           u" :          f                   l
            Ü :    f                    l
            ü :          f                   l
Ordinal       : Is U" or u" the same as Ü or ü?
           U" :    b
           u" :          b
            Ü :                         b
            ü :                              b
IgnoreCase    : Is U" or u" the same as Ü or ü?
           U" :    f                         l
           u" :    f                         l
            Ü :    f                         l
            ü :    f                         l

*/

import System.* ;
import System.Globalization.* ;

public class SamplesCompareInfo
{
       public static void main(String[] args)
    {
        // Creates CompareInfo for the InvariantCulture.
        CompareInfo myComp = 
            CultureInfo.get_InvariantCulture().get_CompareInfo();

        // Searches for the ligature .
        String myStr = "Is AE or ae the same as Æ or æ ?";
        Console.WriteLine();
        Console.WriteLine("No options    : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE"), 
            myComp.LastIndexOf(myStr, "AE"));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae"),
            myComp.LastIndexOf(myStr, "ae"));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ'),
            myComp.LastIndexOf(myStr , 'Æ'));
        PrintMarker("            æ : ", myComp.IndexOf(myStr , 'æ'), 
            myComp.LastIndexOf(myStr, 'æ'));
        Console.WriteLine("Ordinal       : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE",
            CompareOptions.Ordinal));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", 
            CompareOptions.Ordinal));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', 
            CompareOptions.Ordinal));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', 
            CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", 
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", 
            CompareOptions.IgnoreCase));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", 
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", 
            CompareOptions.IgnoreCase));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', 
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', 
            CompareOptions.IgnoreCase));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', 
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', 
            CompareOptions.IgnoreCase));

        // Searches for the combining character sequence Latin capital letter 
        // U with diaeresis or Latin small letter u with diaeresis.
        myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
        Console.WriteLine();
        Console.WriteLine("No options    : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308"),
            myComp.LastIndexOf(myStr, "U\u0308"));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308"), 
            myComp.LastIndexOf(myStr, "u\u0308"));
        PrintMarker("           Ü  : ", myComp.IndexOf(myStr, 'Ü'), 
            myComp.LastIndexOf(myStr, 'Ü'));
        PrintMarker("            ü : ", myComp.IndexOf(myStr,'ü'),
            myComp.LastIndexOf(myStr,'ü'));
        Console.WriteLine("Ordinal       : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308",
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", 
            CompareOptions.Ordinal));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", 
            CompareOptions.Ordinal));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü',
            CompareOptions.Ordinal));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', 
            CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', 
            CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308",
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", 
            CompareOptions.IgnoreCase));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308",
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", 
            CompareOptions.IgnoreCase));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü',
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', 
            CompareOptions.IgnoreCase));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', 
            CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü',
            CompareOptions.IgnoreCase));
    } //main
   
    public static void PrintMarker(String Prefix, int First, int Last)
    {
        // Determines the size of the array to create.
        int mySize;
        if (Last > First) {
            mySize = Last;
        }
        else {
            mySize = First;
        } 
        if (mySize > -1) {
            // Creates an array of Char to hold the markers.
            char myCharArr[] = new char[mySize + 1];
                
            // Inserts the appropriate markers.
            if (First > -1) {
            myCharArr[First] = 'f';
            }
            if (Last > -1) {
                myCharArr[Last] = 'l';
            }
            if (First == Last) {
                myCharArr[First] = 'b';
            }
                
            // Displays the array of Char as a String.
            Console.WriteLine("{0}{1} ", Prefix,new String( myCharArr));
        }
        else {
            Console.WriteLine(Prefix);
        } 
    } //PrintMarker
} //SamplesCompareInfo

/*
This code produces the following output.

No options    : Is AE or ae the same as Æ or æ ?
           AE :    f                    l
           ae :          f                   l
            Æ :    f                    l
            æ :          f                   l
Ordinal       : Is AE or ae the same as Æ or æ ?
           AE :    b
           ae :          b
            Æ :                         b
            æ :                              b
IgnoreCase    : Is AE or ae the same as Æ or æ ?
           AE :    f                         l
           ae :    f                         l
            Æ :    f                         l
            æ :    f                         l

No options    : Is U" or u" the same as Ü or ü?
           U" :    f                    l
           u" :          f                   l
           Ü  :    f                    l
            ü :          f                   l
Ordinal       : Is U" or u" the same as Ü or ü?
           U" :    b
           u" :          b
            Ü :                         b
            ü :                              b
IgnoreCase    : Is U" or u" the same as Ü or ü?
           U" :    f                         l
           u" :    f                         l
            Ü :    f                         l
            ü :    f                         l
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.