This documentation is archived and is not being maintained.

CompareInfo.LastIndexOf Method (String, Char, Int32, CompareOptions)

.NET Framework 1.1

Searches for the specified character and returns the zero-based index of the last occurrence within the section of the source string that extends from the beginning of the string to the specified index using the specified CompareOptions value.

[Visual Basic]
Overloads Public Overridable Function LastIndexOf( _
   ByVal source As String, _
   ByVal value As Char, _
   ByVal startIndex As Integer, _
   ByVal options As CompareOptions _
) As Integer
[C#]
public virtual int LastIndexOf(
 string source,
 char value,
 int startIndex,
 CompareOptions options
);
[C++]
public: virtual int LastIndexOf(
 String* source,
 __wchar_t value,
 int startIndex,
 CompareOptions options
);
[JScript]
public function LastIndexOf(
   source : String,
 value : Char,
 startIndex : int,
 options : CompareOptions
) : int;

Parameters

source
The string to search.
value
The character to locate within source.
startIndex
The zero-based starting index of the backward search.
options
The CompareOptions value that defines how the strings should be compared.

Return Value

The zero-based index of the last occurrence of value within the section of source that extends from the beginning of source to startIndex using the specified CompareOptions value, if found; otherwise, -1.

Exceptions

Exception Type Condition
ArgumentNullException source is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException startIndex is outside the range of valid indexes for source.
ArgumentException options is not a valid CompareOptions value.

Remarks

The source string is searched backward starting at startIndex and ending at the beginning 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.LastIndexOf that search for a char perform an ordinal search, while those that search for a string perform a culture-sensitive search.

Example

[Visual Basic, C#, C++] The following code example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string. Note that IndexOf and LastIndexOf are searching in different portions of the string, even with the same startIndex parameter.

[Visual Basic] 
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Creates CompareInfo for the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' iS is the starting index of the substring.
      Dim [iS] As Integer = 20
      ' myT1 is the string used for padding.
      Dim myT1 As [String]

      ' Searches for the ligature .
      Dim myStr As [String] = "Is AE or ae the same as  or ?"
      Console.WriteLine()

      myT1 = New [String]("-"c, [iS])
      Console.WriteLine("IndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS]), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS]), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS]), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS]), - 1)
      Console.WriteLine("Ordinal       : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.Ordinal), - 1)
      Console.WriteLine("IgnoreCase    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase), - 1)

      myT1 = New [String]("-"c, myStr.Length - [iS] - 1)
      Console.WriteLine("LastIndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS]))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS]))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS]))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS]))
      Console.WriteLine("Ordinal       : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS], CompareOptions.Ordinal))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS], CompareOptions.Ordinal))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.Ordinal))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           AE : ", - 1, myComp.LastIndexOf(myStr, "AE", [iS], CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", - 1, myComp.LastIndexOf(myStr, "ae", [iS], CompareOptions.IgnoreCase))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase))

      ' Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
      myStr = "Is " & ChrW(&H0055) & ChrW(&H0308) & " or " & ChrW(&H0075) & ChrW(&H0308) & " the same as " & ChrW(&H00DC) & " or " & ChrW(&H00FC) & "?"
      Console.WriteLine()

      myT1 = New [String]("-"c, [iS])
      Console.WriteLine("IndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS]), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS]), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS]), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS]), - 1)
      Console.WriteLine("Ordinal       : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.Ordinal), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.Ordinal), - 1)
      Console.WriteLine("IgnoreCase    : {0}{1}", myT1, myStr.Substring([iS]))
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase), - 1)
      PrintMarker("             : ", myComp.IndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase), - 1)

      myT1 = New [String]("-"c, myStr.Length - [iS] - 1)
      Console.WriteLine("LastIndexOf( String, *, {0}, * )", [iS])
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS]))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS]))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS]))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS]))
      Console.WriteLine("Ordinal       : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.Ordinal))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.Ordinal))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}", myStr.Substring(0, [iS] + 1), myT1)
      PrintMarker("           U" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", - 1, myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS], CompareOptions.IgnoreCase))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase))
      PrintMarker("             : ", - 1, myComp.LastIndexOf(myStr, ""c, [iS], CompareOptions.IgnoreCase))

   End Sub 'Main

   Public Shared Sub PrintMarker(Prefix As [String], First As Integer, Last As Integer)

      ' Determines the size of the array to create.
      Dim mySize As Integer
      If Last > First Then
         mySize = Last
      Else
         mySize = First
      End If 

      If mySize > - 1 Then

         ' Creates an array of Char to hold the markers.
         Dim myCharArr(mySize + 1) As [Char]

         ' Inserts the appropriate markers.
         If First > - 1 Then
            myCharArr(First) = "f"c
         End If
         If Last > - 1 Then
            myCharArr(Last) = "l"c
         End If
         If First = Last Then
            myCharArr(First) = "b"c
         End If 

         ' Displays the array of Char as a String.
         Console.WriteLine("{0}{1}", Prefix, New [String](myCharArr))
      
      Else
         Console.WriteLine(Prefix)

      End If 

   End Sub 'PrintMarker

End Class 'SamplesCompareInfo 


'This code produces the following output.
'
'IndexOf( String, *, 20, * )
'Original      : Is AE or ae the same as  or ?
'No options    : -------------------- as  or ?
'           AE :                         f
'           ae :                              f
'             :                         f
'             :                              f
'Ordinal       : -------------------- as  or ?
'           AE :
'           ae :
'             :                         f
'             :                              f
'IgnoreCase    : -------------------- as  or ?
'           AE :                         f
'           ae :                         f
'             :                         f
'             :                         f
'LastIndexOf( String, *, 20, * )
'Original      : Is AE or ae the same as  or ?
'No options    : Is AE or ae the same ----------
'           AE :    l
'           ae :          l
'             :    l
'             :          l
'Ordinal       : Is AE or ae the same ----------
'           AE :    l
'           ae :          l
'             :
'             :
'IgnoreCase    : Is AE or ae the same ----------
'           AE :          l
'           ae :          l
'             :          l
'             :          l
'
'IndexOf( String, *, 20, * )
'Original      : Is U" or u" the same as  or ?
'No options    : -------------------- as  or ?
'           U" :                         f
'           u" :                              f
'             :                         f
'             :                              f
'Ordinal       : -------------------- as  or ?
'           U" :
'           u" :
'             :                         f
'             :                              f
'IgnoreCase    : -------------------- as  or ?
'           U" :                         f
'           u" :                         f
'             :                         f
'             :                         f
'LastIndexOf( String, *, 20, * )
'Original      : Is U" or u" the same as  or ?
'No options    : Is U" or u" the same ----------
'           U" :    l
'           u" :          l
'             :    l
'             :          l
'Ordinal       : Is U" or u" the same ----------
'           U" :    l
'           u" :          l
'             :
'             :
'IgnoreCase    : Is U" or u" the same ----------
'           U" :          l
'           u" :          l
'             :          l
'             :          l


[C#] 
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

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

      // iS is the starting index of the substring.
      int iS = 20;
      // myT1 is the string used for padding.
      String myT1;

      // Searches for the ligature .
      String myStr = "Is AE or ae the same as  or ?";
      Console.WriteLine();

      myT1 = new String( '-', iS );
      Console.WriteLine( "IndexOf( String, *, {0}, * )", iS );
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS ), -1 );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS ), -1 );
      Console.WriteLine( "Ordinal       : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.Ordinal ), -1 );
      Console.WriteLine( "IgnoreCase    : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.IgnoreCase ), -1 );

      myT1 = new String( '-', myStr.Length - iS - 1 );
      Console.WriteLine( "LastIndexOf( String, *, {0}, * )", iS );
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           AE : ", -1, myComp.LastIndexOf( myStr, "AE", iS ) );
      PrintMarker( "           ae : ", -1, myComp.LastIndexOf( myStr, "ae", iS ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS ) );
      Console.WriteLine( "Ordinal       : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           AE : ", -1, myComp.LastIndexOf( myStr, "AE", iS, CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", -1, myComp.LastIndexOf( myStr, "ae", iS, CompareOptions.Ordinal ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.Ordinal ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           AE : ", -1, myComp.LastIndexOf( myStr, "AE", iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", -1, myComp.LastIndexOf( myStr, "ae", iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, 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();

      myT1 = new String( '-', iS );
      Console.WriteLine( "IndexOf( String, *, {0}, * )", iS );
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS ), -1 );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS ), -1 );
      Console.WriteLine( "Ordinal       : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.Ordinal ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.Ordinal ), -1 );
      Console.WriteLine( "IgnoreCase    : {0}{1}", myT1, myStr.Substring( iS ) );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.IgnoreCase ), -1 );
      PrintMarker( "             : ", myComp.IndexOf( myStr, '', iS, CompareOptions.IgnoreCase ), -1 );

      myT1 = new String( '-', myStr.Length - iS - 1 );
      Console.WriteLine( "LastIndexOf( String, *, {0}, * )", iS );
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           U\u0308 : ", -1, myComp.LastIndexOf( myStr, "U\u0308", iS ) );
      PrintMarker( "           u\u0308 : ", -1, myComp.LastIndexOf( myStr, "u\u0308", iS ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS ) );
      Console.WriteLine( "Ordinal       : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           U\u0308 : ", -1, myComp.LastIndexOf( myStr, "U\u0308", iS, CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", -1, myComp.LastIndexOf( myStr, "u\u0308", iS, CompareOptions.Ordinal ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.Ordinal ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}", myStr.Substring( 0, iS + 1 ), myT1 );
      PrintMarker( "           U\u0308 : ", -1, myComp.LastIndexOf( myStr, "U\u0308", iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", -1, myComp.LastIndexOf( myStr, "u\u0308", iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, CompareOptions.IgnoreCase ) );
      PrintMarker( "             : ", -1, myComp.LastIndexOf( myStr, '', iS, 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.

IndexOf( String, *, 20, * )
Original      : Is AE or ae the same as  or ?
No options    : -------------------- as  or ?
           AE :                         f
           ae :                              f
             :                         f
             :                              f
Ordinal       : -------------------- as  or ?
           AE :
           ae :
             :                         f
             :                              f
IgnoreCase    : -------------------- as  or ?
           AE :                         f
           ae :                         f
             :                         f
             :                         f
LastIndexOf( String, *, 20, * )
Original      : Is AE or ae the same as  or ?
No options    : Is AE or ae the same ----------
           AE :    l
           ae :          l
             :    l
             :          l
Ordinal       : Is AE or ae the same ----------
           AE :    l
           ae :          l
             :
             :
IgnoreCase    : Is AE or ae the same ----------
           AE :          l
           ae :          l
             :          l
             :          l

IndexOf( String, *, 20, * )
Original      : Is U" or u" the same as  or ?
No options    : -------------------- as  or ?
           U" :                         f
           u" :                              f
             :                         f
             :                              f
Ordinal       : -------------------- as  or ?
           U" :
           u" :
             :                         f
             :                              f
IgnoreCase    : -------------------- as  or ?
           U" :                         f
           u" :                         f
             :                         f
             :                         f
LastIndexOf( String, *, 20, * )
Original      : Is U" or u" the same as  or ?
No options    : Is U" or u" the same ----------
           U" :    l
           u" :          l
             :    l
             :          l
Ordinal       : Is U" or u" the same ----------
           U" :    l
           u" :          l
             :
             :
IgnoreCase    : Is U" or u" the same ----------
           U" :          l
           u" :          l
             :          l
             :          l

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

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( S"{0}{1}", Prefix, new String( myCharArr ) );

   }
   else
      Console::WriteLine( Prefix );

}


int main()  {

   // Creates CompareInfo for the InvariantCulture.
   CompareInfo* myComp = CultureInfo::InvariantCulture->CompareInfo;

   // iS is the starting index of the substring.
   int iS = 20;
   // myT1 is the string used for padding.
   String* myT1;

   // Searches for the ligature .
   String* myStr = S"Is AE or ae the same as  or ?";
   Console::WriteLine();

   myT1 = new String( '-', iS );
   Console::WriteLine( S"IndexOf( String, *, {0}, * )", __box(iS));
   Console::WriteLine( S"Original      : {0}", myStr );
   Console::WriteLine( S"No options    : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           AE : ", myComp->IndexOf( myStr, S"AE", iS ), -1 );
   PrintMarker( S"           ae : ", myComp->IndexOf( myStr, S"ae", iS ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS ), -1 );
   Console::WriteLine( S"Ordinal       : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           AE : ", myComp->IndexOf( myStr, S"AE", iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"           ae : ", myComp->IndexOf( myStr, S"ae", iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::Ordinal ), -1 );
   Console::WriteLine( S"IgnoreCase    : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           AE : ", myComp->IndexOf( myStr, S"AE", iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"           ae : ", myComp->IndexOf( myStr, S"ae", iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ), -1 );

   myT1 = new String( '-', myStr->Length - iS - 1 );
   Console::WriteLine( S"LastIndexOf( String, *, {0}, * )", __box(iS));
   Console::WriteLine( S"Original      : {0}", myStr );
   Console::WriteLine( S"No options    : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           AE : ", -1, myComp->LastIndexOf( myStr, S"AE", iS ) );
   PrintMarker( S"           ae : ", -1, myComp->LastIndexOf( myStr, S"ae", iS ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS ) );
   Console::WriteLine( S"Ordinal       : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           AE : ", -1, myComp->LastIndexOf( myStr, S"AE", iS, CompareOptions::Ordinal ) );
   PrintMarker( S"           ae : ", -1, myComp->LastIndexOf( myStr, S"ae", iS, CompareOptions::Ordinal ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::Ordinal ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::Ordinal ) );
   Console::WriteLine( S"IgnoreCase    : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           AE : ", -1, myComp->LastIndexOf( myStr, S"AE", iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"           ae : ", -1, myComp->LastIndexOf( myStr, S"ae", iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ) );

   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = S"Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   Console::WriteLine();

   myT1 = new String( '-', iS );
   Console::WriteLine( S"IndexOf( String, *, {0}, * )", __box(iS));
   Console::WriteLine( S"Original      : {0}", myStr );
   Console::WriteLine( S"No options    : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308", iS ), -1 );
   PrintMarker( S"           u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308", iS ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS ), -1 );
   Console::WriteLine( S"Ordinal       : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308", iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"           u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308", iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::Ordinal ), -1 );
   Console::WriteLine( S"IgnoreCase    : {0}{1}", myT1, myStr->Substring( iS ) );
   PrintMarker( S"           U\u0308 : ", myComp->IndexOf( myStr, S"U\u0308", iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"           u\u0308 : ", myComp->IndexOf( myStr, S"u\u0308", iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( S"             : ", myComp->IndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ), -1 );

   myT1 = new String( '-', myStr->Length - iS - 1 );
   Console::WriteLine( S"LastIndexOf( String, *, {0}, * )", __box(iS));
   Console::WriteLine( S"Original      : {0}", myStr );
   Console::WriteLine( S"No options    : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           U\u0308 : ", -1, myComp->LastIndexOf( myStr, S"U\u0308", iS ) );
   PrintMarker( S"           u\u0308 : ", -1, myComp->LastIndexOf( myStr, S"u\u0308", iS ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS ) );
   Console::WriteLine( S"Ordinal       : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           U\u0308 : ", -1, myComp->LastIndexOf( myStr, S"U\u0308", iS, CompareOptions::Ordinal ) );
   PrintMarker( S"           u\u0308 : ", -1, myComp->LastIndexOf( myStr, S"u\u0308", iS, CompareOptions::Ordinal ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::Ordinal ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::Ordinal ) );
   Console::WriteLine( S"IgnoreCase    : {0}{1}", myStr->Substring( 0, iS + 1 ), myT1 );
   PrintMarker( S"           U\u0308 : ", -1, myComp->LastIndexOf( myStr, S"U\u0308", iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"           u\u0308 : ", -1, myComp->LastIndexOf( myStr, S"u\u0308", iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( S"             : ", -1, myComp->LastIndexOf( myStr, L'', iS, CompareOptions::IgnoreCase ) );

}

/*
This code produces the following output.

IndexOf( String, *, 20, * )
Original      : Is AE or ae the same as  or ?
No options    : -------------------- as  or ?
           AE :                         f
           ae :                              f
             :                         f
             :                              f
Ordinal       : -------------------- as  or ?
           AE :
           ae :
             :                         f
             :                              f
IgnoreCase    : -------------------- as  or ?
           AE :                         f
           ae :                         f
             :                         f
             :                         f
LastIndexOf( String, *, 20, * )
Original      : Is AE or ae the same as  or ?
No options    : Is AE or ae the same ----------
           AE :    l
           ae :          l
             :    l
             :          l
Ordinal       : Is AE or ae the same ----------
           AE :    l
           ae :          l
             :
             :
IgnoreCase    : Is AE or ae the same ----------
           AE :          l
           ae :          l
             :          l
             :          l

IndexOf( String, *, 20, * )
Original      : Is U" or u" the same as  or ?
No options    : -------------------- as  or ?
           U" :                         f
           u" :                              f
             :                         f
             :                              f
Ordinal       : -------------------- as  or ?
           U" :
           u" :
             :                         f
             :                              f
IgnoreCase    : -------------------- as  or ?
           U" :                         f
           u" :                         f
             :                         f
             :                         f
LastIndexOf( String, *, 20, * )
Original      : Is U" or u" the same as  or ?
No options    : Is U" or u" the same ----------
           U" :    l
           u" :          l
             :    l
             :          l
Ordinal       : Is U" or u" the same ----------
           U" :    l
           u" :          l
             :
             :
IgnoreCase    : Is U" or u" the same ----------
           U" :          l
           u" :          l
             :          l
             :          l

*/

[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, .NET Compact Framework

See Also

CompareInfo Class | CompareInfo Members | System.Globalization Namespace | CompareInfo.LastIndexOf Overload List | IndexOf | CompareOptions

Show: