CompareInfo.LastIndexOf Metoda

Definicja

Zwraca indeks oparty na zerze ostatniego wystąpienia wartości w ciągu lub w jego części.

Przeciążenia

LastIndexOf(String, String, Int32, Int32, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie przy użyciu określonej CompareOptions wartości.

LastIndexOf(String, Char, Int32, Int32, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie przy użyciu określonej CompareOptions wartości.

LastIndexOf(String, String, Int32, Int32)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie.

LastIndexOf(String, String, Int32, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu przy użyciu określonej CompareOptions wartości.

LastIndexOf(String, Char, Int32, Int32)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie.

LastIndexOf(String, Char, Int32, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu przy użyciu określonej CompareOptions wartości.

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

Wyszukuje ostatnie wystąpienie podciągów w ciągu źródłowym.

LastIndexOf(String, Char)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym.

LastIndexOf(String, String, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym przy użyciu określonej CompareOptions wartości.

LastIndexOf(String, Char, Int32)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu.

LastIndexOf(String, Char, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w całym ciągu źródłowym przy użyciu określonej CompareOptions wartości.

LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

Wyszukuje ostatnie wystąpienie elementu Rune w określonym zakresie znaków tylko do odczytu.

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

Wyszukuje ostatnie wystąpienie podciągów w określonym zakresie znaków tylko do odczytu.

LastIndexOf(String, String)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym.

LastIndexOf(String, String, Int32)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu.

LastIndexOf(String, String, Int32, Int32, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, int count, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, int count, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, string value, int startIndex, int count, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, string value, int startIndex, int count, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * string * int * int * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * string * int * int * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * string * int * int * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As String, startIndex As Integer, count As Integer, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As String, startIndex As Integer, count As Integer, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

count
Int32

Liczba elementów w sekcji do wyszukania.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks zerowy ostatniego valuewystąpienia , jeśli zostanie znaleziony, w sekcji source , która zawiera liczbę elementów określonych przez count i kończy się na startIndex, przy użyciu określonych opcji porównania; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

-lub-

value to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

-lub-

Parametr count ma wartość niższą niż zero.

-lub-

startIndex i count nie należy określać prawidłowej sekcji w pliku source.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = 8;
   
   // iL is the length of the substring.
   int iL = 18;
   
   // myT1 and myT2 are the strings used for padding.
   String^ myT1 = gcnew String( '-',iS );
   String^ myT2;
   
   // Searches for the ligature Æ.
   String^ myStr = "Is AE or ae the same as Æ or æ?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
}

/*
This code produces the following output.

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

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

*/
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 = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String( '-', iS );
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, 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?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, 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.

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

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

*/
Imports System.Globalization

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 = 8
      ' iL is the length of the substring.
      Dim iL As Integer = 18
      ' myT1 and myT2 are the strings used for padding.
      Dim myT1 As New [String]("-"c, [iS])
      Dim myT2 As [String]

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, 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) & "?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'This code produces the following output.
'
'Original      : Is AE or ae the same as Æ or æ?
'No options    : -------- ae the same as Æ -----
'           AE :                         b
'           ae :          b
'            Æ :                         b
'            æ :          b
'Ordinal       : -------- ae the same as Æ -----
'           AE :
'           ae :          b
'            Æ :                         b
'            æ :
'IgnoreCase    : -------- ae the same as Æ -----
'           AE :          f              l
'           ae :          f              l
'            Æ :          f              l
'            æ :          f              l
'
'Original      : Is U" or u" the same as Ü or ü?
'No options    : -------- u" the same as Ü -----
'           U" :                         b
'           u" :          b
'            Ü :                         b
'            ü :          b
'Ordinal       : -------- u" the same as Ü -----
'           U" :
'           u" :          b
'            Ü :                         b
'            ü :
'IgnoreCase    : -------- u" the same as Ü -----
'           U" :          f              l
'           u" :          f              l
'            Ü :          f              l
'            ü :          f              l

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od i kończąc na startIndex + startIndex - count 1.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options wartość zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury), w którym są porównywane wartości Unicode.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę (czyli jeśli options nie Ordinal jest lub OrdinalIgnoreCase), jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value składa się tylko z jednego lub większej liczby znaków LastIndexOf(String, String, Int32, Int32, CompareOptions) , metoda zawsze zwraca startIndexwartość , która jest pozycją znaku, na której rozpoczyna się wyszukiwanie.

W poniższym przykładzie LastIndexOf(String, String, Int32, Int32, CompareOptions) metoda służy do znajdowania pozycji łącznika miękkiego (U+00AD), a następnie znaku "m" we wszystkich, ale pierwszej pozycji znaku przed ostatnim "m" w dwóch ciągach. Tylko jeden z ciągów zawiera wymagany podciąg. W obu przypadkach, ponieważ łącznik nietrwały jest znakiem do pominięcia, metoda ta zwraca indeks „m” w ciągu podczas przeprowadzania porównania z uwzględnieniem ustawień kulturowych Gdy jednak wykonuje porównanie porządkowe, znajduje podciąg tylko w pierwszym ciągu. Należy pamiętać, że w przypadku pierwszego ciągu, który zawiera łącznik miękki, po którym następuje znak "m", metoda nie zwraca indeksu łącznika miękkiego, ale zamiast tego zwraca indeks "m", gdy wykonuje porównanie wrażliwe na kulturę. Metoda ta zwraca indeks łącznika nietrwałego w pierwszym ciągu tylko wtedy, gdy wykonuje porównanie porządkowe.

Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo

      Dim searchString As String = ChrW(&h00AD) + "m"
      Dim s1 As String = "ani" + ChrW(&h00AD) + "m" 
      Dim s2 As String = "animal"
      Dim position As Integer

      position = ci.LastIndexOf(s1, "m"c)
      If position >= 1 Then
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, position, CompareOptions.None))
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, position, CompareOptions.Ordinal))
      End If      
      
      position = ci.LastIndexOf(s2, "m"c)
      If position >= 1 Then 
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, position, CompareOptions.None))
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, position, CompareOptions.Ordinal))
      End If
   End Sub
End Module
' The example displays the following output:
'       4
'       3
'       3
'       -1

Zobacz też

Dotyczy

LastIndexOf(String, Char, Int32, Int32, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, char value, int startIndex, int count, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, char value, int startIndex, int count, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, char value, int startIndex, int count, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, char value, int startIndex, int count, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * char * int * int * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * char * int * int * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * char * int * int * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As Char, startIndex As Integer, count As Integer, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As Char, startIndex As Integer, count As Integer, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

count
Int32

Liczba elementów w sekcji do wyszukania.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks zerowy ostatniego valuewystąpienia , jeśli zostanie znaleziony, w sekcji source , która zawiera liczbę elementów określonych przez count i kończy się na startIndex, przy użyciu określonych opcji porównania; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

-lub-

Parametr count ma wartość niższą niż zero.

-lub-

startIndex i count nie należy określać prawidłowej sekcji w pliku source.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = 8;
   
   // iL is the length of the substring.
   int iL = 18;
   
   // myT1 and myT2 are the strings used for padding.
   String^ myT1 = gcnew String( '-',iS );
   String^ myT2;
   
   // Searches for the ligature Æ.
   String^ myStr = "Is AE or ae the same as Æ or æ?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
}

/*
This code produces the following output.

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

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

*/
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 = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String( '-', iS );
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, 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?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, 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.

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

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

*/
Imports System.Globalization

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 = 8
      ' iL is the length of the substring.
      Dim iL As Integer = 18
      ' myT1 and myT2 are the strings used for padding.
      Dim myT1 As New [String]("-"c, [iS])
      Dim myT2 As [String]

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, 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) & "?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'This code produces the following output.
'
'Original      : Is AE or ae the same as Æ or æ?
'No options    : -------- ae the same as Æ -----
'           AE :                         b
'           ae :          b
'            Æ :                         b
'            æ :          b
'Ordinal       : -------- ae the same as Æ -----
'           AE :
'           ae :          b
'            Æ :                         b
'            æ :
'IgnoreCase    : -------- ae the same as Æ -----
'           AE :          f              l
'           ae :          f              l
'            Æ :          f              l
'            æ :          f              l
'
'Original      : Is U" or u" the same as Ü or ü?
'No options    : -------- u" the same as Ü -----
'           U" :                         b
'           u" :          b
'            Ü :                         b
'            ü :          b
'Ordinal       : -------- u" the same as Ü -----
'           U" :
'           u" :          b
'            Ü :                         b
'            ü :
'IgnoreCase    : -------- u" the same as Ü -----
'           U" :          f              l
'           u" :          f              l
'            Ü :          f              l
'            ü :          f              l

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od i kończąc na startIndex + startIndex - count 1.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury). Znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku LastIndexOf(String, Char, Int32, Int32, CompareOptions) metoda zawsze zwraca wartość startIndex, czyli pozycję znaku, w której rozpoczyna się wyszukiwanie. W poniższym przykładzie LastIndexOf(String, Char, Int32, Int32, CompareOptions) metoda jest używana do znajdowania łącznika miękkiego (U+00AD), który poprzedza ostatni "m" w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik nietrwały jest znakiem do zignorowania, wyszukiwanie wrażliwe na kulturę zwraca pozycję indeksu "m", czyli wartość startIndex. Wyszukiwanie porządkowe jednak pomyślnie znajduje łącznik nietrwały w jednym ciągu i zgłasza, że jest nieobecny w drugim ciągu.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      int position = 0;

      // Find the index of the soft hyphen using culture-sensitive comparison.
      position = ci.LastIndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position,
                           position + 1, CompareOptions.IgnoreCase));

      position = ci.LastIndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position,
                           position + 1, CompareOptions.IgnoreCase));

      // Find the index of the soft hyphen using ordinal comparison.
      position = ci.LastIndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position,
                           position + 1, CompareOptions.Ordinal));

      position = ci.LastIndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position,
                           position + 1, CompareOptions.Ordinal));
   }
}
// The example displays the following output:
//       'm' at position 4
//       4
//       'm' at position 3
//       3
//       'm' at position 4
//       3
//       'm' at position 3
//       -1

Zobacz też

Dotyczy

LastIndexOf(String, String, Int32, Int32)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie.

public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, int count);
public:
 int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, int count);
public virtual int LastIndexOf (string source, string value, int startIndex, int count);
public int LastIndexOf (string source, string value, int startIndex, int count);
abstract member LastIndexOf : string * string * int * int -> int
override this.LastIndexOf : string * string * int * int -> int
member this.LastIndexOf : string * string * int * int -> int
Public Overridable Function LastIndexOf (source As String, value As String, startIndex As Integer, count As Integer) As Integer
Public Function LastIndexOf (source As String, value As String, startIndex As Integer, count As Integer) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

count
Int32

Liczba elementów w sekcji do wyszukania.

Zwraca

Int32

Indeks zerowy ostatniego wystąpienia klasy value, jeśli zostanie znaleziony, w sekcji source , która zawiera liczbę elementów określonych przez count i kończy się na startIndex; w przeciwnym razie - 1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

-lub-

value to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

-lub-

Parametr count ma wartość niższą niż zero.

-lub-

startIndex i count nie należy określać prawidłowej sekcji w pliku source.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = 8;
   
   // iL is the length of the substring.
   int iL = 18;
   
   // myT1 and myT2 are the strings used for padding.
   String^ myT1 = gcnew String( '-',iS );
   String^ myT2;
   
   // Searches for the ligature Æ.
   String^ myStr = "Is AE or ae the same as Æ or æ?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
}

/*
This code produces the following output.

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

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

*/
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 = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String( '-', iS );
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, 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?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, 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.

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

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

*/
Imports System.Globalization

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 = 8
      ' iL is the length of the substring.
      Dim iL As Integer = 18
      ' myT1 and myT2 are the strings used for padding.
      Dim myT1 As New [String]("-"c, [iS])
      Dim myT2 As [String]

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, 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) & "?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'This code produces the following output.
'
'Original      : Is AE or ae the same as Æ or æ?
'No options    : -------- ae the same as Æ -----
'           AE :                         b
'           ae :          b
'            Æ :                         b
'            æ :          b
'Ordinal       : -------- ae the same as Æ -----
'           AE :
'           ae :          b
'            Æ :                         b
'            æ :
'IgnoreCase    : -------- ae the same as Æ -----
'           AE :          f              l
'           ae :          f              l
'            Æ :          f              l
'            æ :          f              l
'
'Original      : Is U" or u" the same as Ü or ü?
'No options    : -------- u" the same as Ü -----
'           U" :                         b
'           u" :          b
'            Ü :                         b
'            ü :          b
'Ordinal       : -------- u" the same as Ü -----
'           U" :
'           u" :          b
'            Ü :                         b
'            ü :
'IgnoreCase    : -------- u" the same as Ü -----
'           U" :          f              l
'           u" :          f              l
'            Ü :          f              l
'            ü :          f              l

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od i kończąc na startIndex + startIndex - count 1.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), w którym porównywane są wartości Unicode, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć Ordinal wartości.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń .

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value składa się tylko z jednego lub większej liczby znaków LastIndexOf(String, String, Int32, Int32) , metoda zawsze zwraca startIndexwartość , która jest pozycją znaku, na której rozpoczyna się wyszukiwanie.

W poniższym przykładzie LastIndexOf(String, String, Int32, Int32) metoda służy do znajdowania pozycji łącznika miękkiego (U+00AD), a następnie znaku "m" w trzech znakach poprzedzających ostatnie "m" dwóch ciągów. Tylko jeden z ciągów zawiera wymagany podciąg. W obu przypadkach, ponieważ łącznik nietrwały jest znakiem do pominięcia, metoda ta zwraca indeks „m” w ciągu podczas przeprowadzania porównania z uwzględnieniem ustawień kulturowych Należy pamiętać, że w przypadku pierwszego ciągu, który zawiera łącznik nietrwały, a po nim „m”, metoda nie zwraca indeksu łącznika nietrwałego, ale zamiast tego zwraca indeks „m”.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string searchString = "\u00ADm";
      string s1 = "ani\u00ADmal" ;
      string s2 = "animal";
      int position;

      position = ci.LastIndexOf(s1, 'm');
      if (position >= 0) {
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, 3, CompareOptions.None));
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, 3, CompareOptions.Ordinal));
      }

      position = ci.LastIndexOf(s2, 'm');
      if (position >= 0) {
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, 3, CompareOptions.None));
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, 3, CompareOptions.Ordinal));
      }
   }
}
// The example displays the following output:
//       4
//       3
//       3
//       -1

Zobacz też

Dotyczy

LastIndexOf(String, String, Int32, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, string value, int startIndex, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, string value, int startIndex, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * string * int * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * string * int * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * string * int * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As String, startIndex As Integer, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As String, startIndex As Integer, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia klasy , jeśli zostanie znaleziony, w sekcji, source która rozciąga się od początku source do startIndex, przy użyciu określonych opcji porównania; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

-lub-

value to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu. Zwróć uwagę, że IndexOf i LastIndexOf wyszukują w różnych fragmentach ciągu, nawet przy użyciu tego samego startIndex parametru.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = "Is AE or ae the same as Æ or æ?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Æ', iS ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Æ', iS ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", -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 = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Ü', iS ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Ü', iS ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", -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

*/
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

*/
Imports System.Globalization

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

   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

End Class


'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

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od startIndex i kończąc na początku ciągu.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options wartość zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury), w którym są porównywane wartości Unicode.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę (czyli jeśli options nie Ordinal jest lub OrdinalIgnoreCase), jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value składa się tylko z jednego lub większej liczby znaków LastIndexOf(String, String, Int32, CompareOptions) , metoda zawsze zwraca startIndexwartość , która jest pozycją znaku, na której rozpoczyna się wyszukiwanie.

W poniższym przykładzie LastIndexOf(String, String, Int32, CompareOptions) metoda służy do znajdowania pozycji łącznika miękkiego (U+00AD), po którym następuje znak "m", rozpoczynając od końcowego "m" w dwóch ciągach. Tylko jeden z ciągów zawiera wymagany podciąg. W obu przypadkach, ponieważ łącznik nietrwały jest znakiem do pominięcia, metoda ta zwraca indeks „m” w ciągu podczas przeprowadzania porównania z uwzględnieniem ustawień kulturowych Należy pamiętać, że w przypadku pierwszego ciągu, który zawiera łącznik nietrwały, a po nim „m”, metoda nie zwraca indeksu łącznika nietrwałego, ale zamiast tego zwraca indeks „m”. Metoda ta zwraca indeks łącznika nietrwałego w pierwszym ciągu tylko wtedy, gdy wykonuje porównanie porządkowe.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string searchString = "\u00ADm";
      string s1 = "ani\u00ADmal" ;
      string s2 = "animal";
      int position;

      position = ci.LastIndexOf(s1, 'm');
      if (position >= 0) {
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.None));
         Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.Ordinal));
      }

      position = ci.LastIndexOf(s2, 'm');
      if (position >= 0) {
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.None));
         Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.Ordinal));
      }
   }
}
// The example displays the following output:
//       4
//       3
//       3
//       -1

Zobacz też

Dotyczy

LastIndexOf(String, Char, Int32, Int32)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który zawiera określoną liczbę elementów i kończy się na określonym indeksie.

public:
 virtual int LastIndexOf(System::String ^ source, char value, int startIndex, int count);
public:
 int LastIndexOf(System::String ^ source, char value, int startIndex, int count);
public virtual int LastIndexOf (string source, char value, int startIndex, int count);
public int LastIndexOf (string source, char value, int startIndex, int count);
abstract member LastIndexOf : string * char * int * int -> int
override this.LastIndexOf : string * char * int * int -> int
member this.LastIndexOf : string * char * int * int -> int
Public Overridable Function LastIndexOf (source As String, value As Char, startIndex As Integer, count As Integer) As Integer
Public Function LastIndexOf (source As String, value As Char, startIndex As Integer, count As Integer) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

count
Int32

Liczba elementów w sekcji do wyszukania.

Zwraca

Int32

Indeks zerowy ostatniego wystąpienia klasy value, jeśli zostanie znaleziony, w sekcji source , która zawiera liczbę elementów określonych przez count i kończy się na startIndex; w przeciwnym razie - 1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

-lub-

Parametr count ma wartość niższą niż zero.

-lub-

startIndex i count nie należy określać prawidłowej sekcji w pliku source.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = 8;
   
   // iL is the length of the substring.
   int iL = 18;
   
   // myT1 and myT2 are the strings used for padding.
   String^ myT1 = gcnew String( '-',iS );
   String^ myT2;
   
   // Searches for the ligature Æ.
   String^ myStr = "Is AE or ae the same as Æ or æ?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           AE : ", myComp->IndexOf( myStr, "AE", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           ae : ", myComp->IndexOf( myStr, "ae", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Æ : ", myComp->IndexOf( myStr, L'Æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   myT2 = gcnew String( '-',myStr->Length - iS - iL );
   Console::WriteLine();
   Console::WriteLine( "Original      : {0}", myStr );
   Console::WriteLine( "No options    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL ) );
   Console::WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::Ordinal ) );
   Console::WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr->Substring( iS, iL ), myT2 );
   PrintMarker( "           U\u0308 : ", myComp->IndexOf( myStr, "U\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "           u\u0308 : ", myComp->IndexOf( myStr, "u\u0308", iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            Ü : ", myComp->IndexOf( myStr, L'Ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, iL, CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', iS + iL - 1, iL, CompareOptions::IgnoreCase ) );
}

/*
This code produces the following output.

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

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

*/
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 = 8;
      // iL is the length of the substring.
      int iL = 18;
      // myT1 and myT2 are the strings used for padding.
      String myT1 = new String( '-', iS );
      String myT2;

      // Searches for the ligature Æ.
      String myStr = "Is AE or ae the same as Æ or æ?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           AE : ", myComp.IndexOf( myStr, "AE", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           ae : ", myComp.IndexOf( myStr, "ae", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Æ : ", myComp.IndexOf( myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            æ : ", myComp.IndexOf( myStr, 'æ', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'æ', iS + iL - 1, iL, 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?";
      myT2 = new String( '-', myStr.Length - iS - iL );
      Console.WriteLine();
      Console.WriteLine( "Original      : {0}", myStr );
      Console.WriteLine( "No options    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL ) );
      Console.WriteLine( "Ordinal       : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.Ordinal ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal ) );
      Console.WriteLine( "IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring( iS, iL ), myT2 );
      PrintMarker( "           U\u0308 : ", myComp.IndexOf( myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "           u\u0308 : ", myComp.IndexOf( myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            Ü : ", myComp.IndexOf( myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase ) );
      PrintMarker( "            ü : ", myComp.IndexOf( myStr, 'ü', iS, iL, CompareOptions.IgnoreCase ), myComp.LastIndexOf( myStr, 'ü', iS + iL - 1, iL, 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.

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

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

*/
Imports System.Globalization

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 = 8
      ' iL is the length of the substring.
      Dim iL As Integer = 18
      ' myT1 and myT2 are the strings used for padding.
      Dim myT1 As New [String]("-"c, [iS])
      Dim myT2 As [String]

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "Is AE or ae the same as Æ or æ?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Æ : ", myComp.IndexOf(myStr, "Æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, [iS] + iL - 1, iL, 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) & "?"
      myT2 = New [String]("-"c, myStr.Length - [iS] - iL)
      Console.WriteLine()
      Console.WriteLine("Original      : {0}", myStr)
      Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL))
      Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring([iS], iL), myT2)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, [iS], iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, [iS] + iL - 1, iL, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'This code produces the following output.
'
'Original      : Is AE or ae the same as Æ or æ?
'No options    : -------- ae the same as Æ -----
'           AE :                         b
'           ae :          b
'            Æ :                         b
'            æ :          b
'Ordinal       : -------- ae the same as Æ -----
'           AE :
'           ae :          b
'            Æ :                         b
'            æ :
'IgnoreCase    : -------- ae the same as Æ -----
'           AE :          f              l
'           ae :          f              l
'            Æ :          f              l
'            æ :          f              l
'
'Original      : Is U" or u" the same as Ü or ü?
'No options    : -------- u" the same as Ü -----
'           U" :                         b
'           u" :          b
'            Ü :                         b
'            ü :          b
'Ordinal       : -------- u" the same as Ü -----
'           U" :
'           u" :          b
'            Ü :                         b
'            ü :
'IgnoreCase    : -------- u" the same as Ü -----
'           U" :          f              l
'           u" :          f              l
'            Ü :          f              l
'            ü :          f              l

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od i kończąc na startIndex + startIndex - count 1.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), gdzie znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć Ordinal wartości. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku LastIndexOf(String, Char, Int32, Int32) metoda zawsze zwraca wartość startIndex, czyli pozycję znaku, w której rozpoczyna się wyszukiwanie. W poniższym przykładzie LastIndexOf(String, Char, Int32, Int32) metoda jest używana do znajdowania łącznika miękkiego (U+00AD), który poprzedza ostatni "m" w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik miękki jest znakiem, który można zignorować, metoda zwraca pozycję indeksu "m", czyli wartość startIndex.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      int position = 0;

      // Find the index of the soft hyphen.
      position = ci.IndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position, position + 1));

      position = ci.IndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position, position + 1));
   }
}
// The example displays the following output:
//       'm' at position 4
//       4
//       'm' at position 3
//       3

Zobacz też

Dotyczy

LastIndexOf(String, Char, Int32, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, char value, int startIndex, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, char value, int startIndex, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, char value, int startIndex, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, char value, int startIndex, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * char * int * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * char * int * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * char * int * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As Char, startIndex As Integer, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As Char, startIndex As Integer, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia klasy , jeśli zostanie znaleziony, w sekcji, source która rozciąga się od początku source do startIndex, przy użyciu określonych opcji porównania; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu. Zwróć uwagę, że IndexOf i LastIndexOf wyszukują w różnych fragmentach ciągu, nawet przy użyciu tego samego startIndex parametru.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = "Is AE or ae the same as Æ or æ?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Æ', iS ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Æ', iS ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", -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 = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Ü', iS ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Ü', iS ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", -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

*/
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

*/
Imports System.Globalization

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

   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

End Class


'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

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od startIndex i kończąc na początku ciągu.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury). Znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku LastIndexOf(String, Char, Int32, CompareOptions) metoda zawsze zwraca wartość startIndex, czyli pozycję znaku, w której rozpoczyna się wyszukiwanie. W poniższym przykładzie LastIndexOf(String, Char, Int32, CompareOptions) metoda jest używana do znajdowania łącznika miękkiego (U+00AD), który poprzedza ostatni "m" w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik miękki jest znakiem ignorowalnym, wyszukiwanie wrażliwe na kulturę zwraca pozycję indeksu "m". Wyszukiwanie porządkowe jednak pomyślnie znajduje łącznik nietrwały w jednym ciągu i zgłasza, że jest nieobecny w drugim ciągu.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      int position = 0;

      // Find the index of the soft hyphen using culture-sensitive comparison.
      position = ci.LastIndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position, CompareOptions.IgnoreCase));

      position = ci.LastIndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position, CompareOptions.IgnoreCase));

      // Find the index of the soft hyphen using ordinal comparison.
      position = ci.LastIndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position, CompareOptions.Ordinal));

      position = ci.LastIndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position, CompareOptions.Ordinal));
   }
}
// The example displays the following output:
//       'm' at position 4
//       4
//       'm' at position 3
//       3
//       'm' at position 4
//       3
//       'm' at position 3
//       -1

Zobacz też

Dotyczy

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

Wyszukuje ostatnie wystąpienie podciągów w ciągu źródłowym.

public:
 int LastIndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, System::Globalization::CompareOptions options, [Runtime::InteropServices::Out] int % matchLength);
public int LastIndexOf (ReadOnlySpan<char> source, ReadOnlySpan<char> value, System.Globalization.CompareOptions options, out int matchLength);
member this.LastIndexOf : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions * int -> int
Public Function LastIndexOf (source As ReadOnlySpan(Of Char), value As ReadOnlySpan(Of Char), options As CompareOptions, ByRef matchLength As Integer) As Integer

Parametry

source
ReadOnlySpan<Char>

Zakres znaków tylko do odczytu do przeszukiwania.

value
ReadOnlySpan<Char>

Zakres znaków tylko do odczytu zawierający podciąg do zlokalizowania w obiekcie source.

options
CompareOptions

Element CompareOptions do użycia podczas wyszukiwania.

matchLength
Int32

Gdy ta metoda zwraca wartość, zawiera liczbę znaków source , które pasują do żądanej wartości. Może to być inne niż długość value w przypadku przeprowadzenia porównania językowego. Ustaw wartość 0, jeśli value nie można odnaleźć w elemecie source.

Zwraca

Int32

Indeks oparty na zera, do source którego ostatnio pojawia się podciąg value , lub liczbę ujemną, jeśli value nie można odnaleźć w obrębie source.

Wyjątki

options zawiera nieobsługiwaną kombinację flag.

Uwagi

Ta metoda ma większe obciążenie niż inne LastIndexOf przeciążenia, które nie przyjmują argumentu matchLength . Wywołaj to przeciążenie tylko wtedy, gdy wymagane są informacje o długości dopasowania.

Dotyczy

LastIndexOf(String, Char)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym.

public:
 virtual int LastIndexOf(System::String ^ source, char value);
public:
 int LastIndexOf(System::String ^ source, char value);
public virtual int LastIndexOf (string source, char value);
public int LastIndexOf (string source, char value);
abstract member LastIndexOf : string * char -> int
override this.LastIndexOf : string * char -> int
member this.LastIndexOf : string * char -> int
Public Overridable Function LastIndexOf (source As String, value As Char) As Integer
Public Function LastIndexOf (source As String, value As Char) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia , jeśli zostanie znaleziony, w ciągu source; w przeciwnym razie -1.

Wyjątki

source to null.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew String( myCharArr ) );
   }
   else
      Console::WriteLine( Prefix );
}

int 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, L'Æ' ), myComp->LastIndexOf( myStr, L'Æ' ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ' ), myComp->LastIndexOf( myStr, L'æ' ) );
   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, L'Æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\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, L'Ü' ), myComp->LastIndexOf( myStr, L'Ü' ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü' ), myComp->LastIndexOf( myStr, L'ü' ) );
   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, L'Ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::IgnoreCase ) );
}

/*
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

*/
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

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

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

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "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, "Æ"c), myComp.LastIndexOf(myStr, "Æ"c))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c), myComp.LastIndexOf(myStr, "æ"c))
      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, "Æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, 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, "Æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, 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()
      Console.WriteLine("No options    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308)))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308)))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c), myComp.LastIndexOf(myStr, "Ü"c))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c), myComp.LastIndexOf(myStr, "ü"c))
      Console.WriteLine("Ordinal       : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'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

Uwagi

Ciąg źródłowy jest przeszukiwany wstecz, zaczynając od końca ciągu i kończąc na początku ciągu.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), gdzie znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć Ordinal wartości. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku LastIndexOf(String, Char) metoda zawsze zwraca ostatnią pozycję indeksu w source , aby wskazać, że dopasowanie znajduje się na końcu .source W poniższym przykładzie LastIndexOf(String, Char) metoda służy do znajdowania łącznika miękkiego (U+00AD) w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik miękki jest znakiem, który można zignorować, metoda zwraca ostatnią pozycję indeksu w ciągu, aby wskazać, że znalazł dopasowanie na końcu ciągu.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the index of the last soft hyphen.
      Console.WriteLine(ci.LastIndexOf(s1, '\u00AD'));
      Console.WriteLine(ci.LastIndexOf(s2, '\u00AD'));
   }
}
// The example displays the following output:
//       6
//       5

Zobacz też

Dotyczy

LastIndexOf(String, String, CompareOptions)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, System::String ^ value, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, string value, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, string value, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * string * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * string * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * string * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As String, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As String, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia , jeśli zostanie znaleziony, w ciągu source, przy użyciu określonych opcji porównania; w przeciwnym razie -1.

Wyjątki

source to null.

-lub-

value to null.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew String( myCharArr ) );
   }
   else
      Console::WriteLine( Prefix );
}

int 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, L'Æ' ), myComp->LastIndexOf( myStr, L'Æ' ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ' ), myComp->LastIndexOf( myStr, L'æ' ) );
   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, L'Æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\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, L'Ü' ), myComp->LastIndexOf( myStr, L'Ü' ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü' ), myComp->LastIndexOf( myStr, L'ü' ) );
   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, L'Ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::IgnoreCase ) );
}

/*
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

*/
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

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

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

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "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, "Æ"c), myComp.LastIndexOf(myStr, "Æ"c))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c), myComp.LastIndexOf(myStr, "æ"c))
      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, "Æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, 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, "Æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, 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()
      Console.WriteLine("No options    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308)))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308)))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c), myComp.LastIndexOf(myStr, "Ü"c))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c), myComp.LastIndexOf(myStr, "ü"c))
      Console.WriteLine("Ordinal       : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'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

Uwagi

Ciąg źródłowy jest przeszukiwany wstecz, zaczynając od końca ciągu i kończąc na początku ciągu.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options wartość zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury), w którym są porównywane wartości Unicode.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę (czyli jeśli options nie Ordinal jest lub OrdinalIgnoreCase), jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value zawiera tylko jeden lub więcej znaków, które można zignorować, LastIndexOf(String, String, CompareOptions) metoda zawsze zwraca wartość source.Length - 1, który reprezentuje ostatnią pozycję indeksu w .source W poniższym przykładzie LastIndexOf(String, String, CompareOptions) metoda służy do znajdowania trzech podciągów (łącznika miękkiego (U+00AD), łącznika miękkiego, po którym następuje "n" i łącznika miękkiego, po którym następuje "m") w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. Ponieważ łącznik nietrwały jest znakiem pomijalnym, wyszukiwanie wrażliwe na kulturę zwraca tę samą wartość, którą zwróci, jeśli łącznik miękki nie został uwzględniony w ciągu wyszukiwania. Wyszukiwanie porządkowe jednak pomyślnie znajduje łącznik nietrwały w jednym ciągu i zgłasza, że jest nieobecny w drugim ciągu.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      Console.WriteLine("Culture-sensitive comparison:");
      // Use culture-sensitive comparison to find the last soft hyphen.
      Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", CompareOptions.None));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", CompareOptions.None));

      // Use culture-sensitive comparison to find the last soft hyphen followed by "n".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", CompareOptions.None));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", CompareOptions.None));

      // Use culture-sensitive comparison to find the last soft hyphen followed by "m".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", CompareOptions.None));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", CompareOptions.None));

      Console.WriteLine("Ordinal comparison:");
      // Use ordinal comparison to find the last soft hyphen.
      Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", CompareOptions.Ordinal));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", CompareOptions.Ordinal));

      // Use ordinal comparison to find the last soft hyphen followed by "n".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", CompareOptions.Ordinal));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", CompareOptions.Ordinal));

      // Use ordinal comparison to find the last soft hyphen followed by "m".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", CompareOptions.Ordinal));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", CompareOptions.Ordinal));
   }
}
// The example displays the following output:
//       6
//       5
//       1
//       1
//       4
//       3
//       Ordinal comparison:
//       3
//       -1
//       -1
//       -1
//       3
//       -1

Zobacz też

Dotyczy

LastIndexOf(String, Char, Int32)

Wyszukuje określony znak i zwraca indeks oparty na zerze ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu.

public:
 int LastIndexOf(System::String ^ source, char value, int startIndex);
public:
 virtual int LastIndexOf(System::String ^ source, char value, int startIndex);
public int LastIndexOf (string source, char value, int startIndex);
public virtual int LastIndexOf (string source, char value, int startIndex);
member this.LastIndexOf : string * char * int -> int
abstract member LastIndexOf : string * char * int -> int
override this.LastIndexOf : string * char * int -> int
Public Function LastIndexOf (source As String, value As Char, startIndex As Integer) As Integer
Public Overridable Function LastIndexOf (source As String, value As Char, startIndex As Integer) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

Zwraca

Int32

Indeks zerowy ostatniego valuewystąpienia klasy , jeśli zostanie znaleziony, w sekcji tej source rozciąga się od początku source do startIndex; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu. Zwróć uwagę, że IndexOf i LastIndexOf wyszukują w różnych fragmentach ciągu, nawet przy użyciu tego samego startIndex parametru.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = "Is AE or ae the same as Æ or æ?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Æ', iS ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Æ', iS ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", -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 = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Ü', iS ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Ü', iS ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", -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

*/
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

*/
Imports System.Globalization

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

   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

End Class


'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

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od startIndex i kończąc na początku ciągu.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), gdzie znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć Ordinal wartości. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku LastIndexOf(String, Char, Int32) metoda zawsze zwraca wartość startIndex, czyli pozycję znaku, w której rozpoczyna się wyszukiwanie. W poniższym przykładzie LastIndexOf(String, Char, Int32) metoda jest używana do znajdowania łącznika miękkiego (U+00AD), który poprzedza ostatni "m" w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik miękki jest znakiem, który można zignorować, metoda zwraca pozycję indeksu "m", czyli wartość startIndex.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      int position = 0;

      // Find the last index of the soft hyphen.
      position = ci.LastIndexOf(s1, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position));

      position = ci.LastIndexOf(s2, 'm');
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position));
   }
}
// The example displays the following output:
//       'm' at position 4
//       4
//       'm' at position 3

Zobacz też

Dotyczy

LastIndexOf(String, Char, CompareOptions)

Wyszukuje określony znak i zwraca indeks zerowy ostatniego wystąpienia w całym ciągu źródłowym przy użyciu określonej CompareOptions wartości.

public:
 virtual int LastIndexOf(System::String ^ source, char value, System::Globalization::CompareOptions options);
public:
 int LastIndexOf(System::String ^ source, char value, System::Globalization::CompareOptions options);
public virtual int LastIndexOf (string source, char value, System.Globalization.CompareOptions options);
public int LastIndexOf (string source, char value, System.Globalization.CompareOptions options);
abstract member LastIndexOf : string * char * System.Globalization.CompareOptions -> int
override this.LastIndexOf : string * char * System.Globalization.CompareOptions -> int
member this.LastIndexOf : string * char * System.Globalization.CompareOptions -> int
Public Overridable Function LastIndexOf (source As String, value As Char, options As CompareOptions) As Integer
Public Function LastIndexOf (source As String, value As Char, options As CompareOptions) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
Char

Znak do zlokalizowania w obiekcie source.

options
CompareOptions

Wartość definiująca sposób source i value elementy, które powinny być porównywane. optionsto wartość Ordinalwyliczenia lub bitowa kombinacja co najmniej jednej z następujących wartości: IgnoreCase, , IgnoreSymbolsIgnoreNonSpace, IgnoreWidthi IgnoreKanaType.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia , jeśli zostanie znaleziony, w ciągu source, przy użyciu określonych opcji porównania; w przeciwnym razie -1.

Wyjątki

source to null.

options zawiera nieprawidłową CompareOptions wartość.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew String( myCharArr ) );
   }
   else
      Console::WriteLine( Prefix );
}

int 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, L'Æ' ), myComp->LastIndexOf( myStr, L'Æ' ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ' ), myComp->LastIndexOf( myStr, L'æ' ) );
   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, L'Æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\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, L'Ü' ), myComp->LastIndexOf( myStr, L'Ü' ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü' ), myComp->LastIndexOf( myStr, L'ü' ) );
   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, L'Ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::IgnoreCase ) );
}

/*
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

*/
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

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

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

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "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, "Æ"c), myComp.LastIndexOf(myStr, "Æ"c))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c), myComp.LastIndexOf(myStr, "æ"c))
      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, "Æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, 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, "Æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, 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()
      Console.WriteLine("No options    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308)))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308)))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c), myComp.LastIndexOf(myStr, "Ü"c))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c), myComp.LastIndexOf(myStr, "ü"c))
      Console.WriteLine("Ordinal       : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'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

Uwagi

Ciąg źródłowy jest przeszukiwany wstecz, zaczynając od końca ciągu i kończąc na początku ciągu.

Wartość jest nieprawidłowa CompareOptions.StringSort dla tej metody.

Jeśli options nie zawiera Ordinal wartości, to przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Jeśli znak jest wartością Unicode reprezentującą wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu jego składników w prawidłowej sekwencji, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Jeśli options zawiera Ordinal wartość, to przeciążenie wykonuje wyszukiwanie porządkowe (bez uwzględniania kultury). Znak jest uznawany za równoważny innemu znakowi tylko wtedy, gdy wartości Unicode są takie same. Przeciążenia tego String.LastIndexOf wyszukiwania dla znaku wykonują wyszukiwanie porządkowe, podczas gdy te, które wyszukują ciąg, wykonują wyszukiwanie wrażliwe na kulturę.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value jest znakiem, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. W tym przypadku metoda zawsze zwraca ostatnią pozycję znaku w source , LastIndexOf(String, Char, CompareOptions) aby wskazać, że dopasowanie znajduje się na końcu .source W poniższym przykładzie LastIndexOf(String, Char, CompareOptions) metoda jest używana do wyszukiwania łącznika miękkiego (U+00AD) w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W obu przypadkach, ponieważ łącznik miękki jest znakiem nie do pominiania, wyszukiwanie wrażliwe na kulturę zwraca ostatnią pozycję indeksu w ciągu źródłowym. Z kolei wyszukiwanie porządkowe pomyślnie znajduje łącznik nietrwały w jednym ciągu i zgłasza, że jest nieobecny w drugim ciągu.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the last index of the soft hyphen using culture-sensitive comparison.
      Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', CompareOptions.IgnoreCase));
      Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', CompareOptions.IgnoreCase));

      // Find the last index of the soft hyphen using ordinal comparison.
      Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', CompareOptions.Ordinal));
      Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', CompareOptions.Ordinal));
   }
}
// The example displays the following output:
//       6
//       5
//       3
//       -1

Zobacz też

Dotyczy

LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

Wyszukuje ostatnie wystąpienie elementu Rune w określonym zakresie znaków tylko do odczytu.

public int LastIndexOf (ReadOnlySpan<char> source, System.Text.Rune value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.LastIndexOf : ReadOnlySpan<char> * System.Text.Rune * System.Globalization.CompareOptions -> int
Public Function LastIndexOf (source As ReadOnlySpan(Of Char), value As Rune, Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Integer

Parametry

source
ReadOnlySpan<Char>

Zakres znaków tylko do odczytu do przeszukiwania.

value
Rune

Element Rune do zlokalizowania w obiekcie source.

options
CompareOptions

Opcjonalna kombinacja CompareOptions wartości wyliczenia do użycia podczas wyszukiwania. Wartość domyślna to None.

Zwraca

Int32

Indeks oparty na zerze do source miejsca value ostatniego pojawia się; lub wartość ujemna, jeśli value nie można odnaleźć w obrębie source.

Wyjątki

options zawiera nieobsługiwaną kombinację flag.

Dotyczy

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

Wyszukuje ostatnie wystąpienie podciągów w określonym zakresie znaków tylko do odczytu.

public int LastIndexOf (ReadOnlySpan<char> source, ReadOnlySpan<char> value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None);
member this.LastIndexOf : ReadOnlySpan<char> * ReadOnlySpan<char> * System.Globalization.CompareOptions -> int
Public Function LastIndexOf (source As ReadOnlySpan(Of Char), value As ReadOnlySpan(Of Char), Optional options As CompareOptions = System.Globalization.CompareOptions.None) As Integer

Parametry

source
ReadOnlySpan<Char>

Zakres znaków tylko do odczytu do przeszukiwania.

value
ReadOnlySpan<Char>

Podciąg do zlokalizowania w obiekcie source.

options
CompareOptions

Opcjonalna kombinacja CompareOptions wartości wyliczenia do użycia podczas wyszukiwania. Wartość domyślna to None.

Zwraca

Int32

Indeks oparty na zera, do source którego ostatnio pojawia się podciąg value , lub wartość ujemna, jeśli value nie można odnaleźć w obrębie source.

Wyjątki

options zawiera nieobsługiwaną kombinację flag.

Dotyczy

LastIndexOf(String, String)

Wyszukuje określony podciąg i zwraca indeks oparty na zerze ostatniego wystąpienia w całym ciągu źródłowym.

public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value);
public:
 int LastIndexOf(System::String ^ source, System::String ^ value);
public virtual int LastIndexOf (string source, string value);
public int LastIndexOf (string source, string value);
abstract member LastIndexOf : string * string -> int
override this.LastIndexOf : string * string -> int
member this.LastIndexOf : string * string -> int
Public Overridable Function LastIndexOf (source As String, value As String) As Integer
Public Function LastIndexOf (source As String, value As String) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

Zwraca

Int32

Indeks oparty na zerze ostatniego valuewystąpienia , jeśli zostanie znaleziony, w ciągu source; w przeciwnym razie -1.

Wyjątki

source to null.

-lub-

value to null.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w ciągu.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew String( myCharArr ) );
   }
   else
      Console::WriteLine( Prefix );
}

int 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, L'Æ' ), myComp->LastIndexOf( myStr, L'Æ' ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ' ), myComp->LastIndexOf( myStr, L'æ' ) );
   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, L'Æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Æ', CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'æ', CompareOptions::IgnoreCase ) );
   
   // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
   myStr = "Is U\u0308 or u\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, L'Ü' ), myComp->LastIndexOf( myStr, L'Ü' ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü' ), myComp->LastIndexOf( myStr, L'ü' ) );
   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, L'Ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::Ordinal ), myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'Ü', CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', CompareOptions::IgnoreCase ), myComp->LastIndexOf( myStr, L'ü', CompareOptions::IgnoreCase ) );
}

/*
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

*/
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

*/
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

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

      ' Searches for the ligature Æ.
      Dim myStr As [String] = "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, "Æ"c), myComp.LastIndexOf(myStr, "Æ"c))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c), myComp.LastIndexOf(myStr, "æ"c))
      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, "Æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.Ordinal))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "æ"c, 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, "Æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Æ"c, CompareOptions.IgnoreCase))
      PrintMarker("            æ : ", myComp.IndexOf(myStr, "æ"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "æ"c, 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()
      Console.WriteLine("No options    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308)))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308)), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308)))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c), myComp.LastIndexOf(myStr, "Ü"c))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c), myComp.LastIndexOf(myStr, "ü"c))
      Console.WriteLine("Ordinal       : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.Ordinal))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.Ordinal))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.Ordinal))
      Console.WriteLine("IgnoreCase    : {0}", myStr)
      PrintMarker("           U" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("           u" & ChrW(&H0308) & " : ", myComp.IndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u" & ChrW(&H0308), CompareOptions.IgnoreCase))
      PrintMarker("            Ü : ", myComp.IndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "Ü"c, CompareOptions.IgnoreCase))
      PrintMarker("            ü : ", myComp.IndexOf(myStr, "ü"c, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ü"c, CompareOptions.IgnoreCase))

   End Sub

   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

End Class


'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

Uwagi

Ciąg źródłowy jest przeszukiwany wstecz, zaczynając od końca ciągu i kończąc na początku ciągu.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), w którym porównywane są wartości Unicode, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć CompareOptions.Ordinal wartości.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value zawiera tylko jeden lub więcej znaków, które można zignorować, LastIndexOf(String, String) metoda zawsze zwraca wartość source.Length - 1, który reprezentuje ostatnią pozycję indeksu w .source W poniższym przykładzie LastIndexOf(String, String) metoda służy do znajdowania trzech podciągów (łącznika miękkiego (U+00AD), łącznika miękkiego, po którym następuje "n" i łącznika miękkiego, po którym następuje "m") w dwóch ciągach. Tylko jeden z ciągów zawiera łącznik nietrwały. W każdym przypadku, ponieważ łącznik miękki jest znakiem, który można zignorować, wynik jest taki sam, jak w przypadku, gdy łącznik miękki nie został uwzględniony w .value Podczas wyszukiwania tylko łącznika nietrwałego metoda zwraca wartość 6 i 5. Te wartości odpowiadają indeksowi ostatniego znaku w dwóch ciągach.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the index of the last soft hyphen.
      Console.WriteLine(ci.LastIndexOf(s1, "\u00AD"));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00AD"));

      // Find the index of the last soft hyphen followed by "n".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn"));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn"));

      // Find the index of the last soft hyphen followed by "m".
      Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm"));
      Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm"));
   }
}
// The example displays the following output:
//       6
//       5
//       1
//       1
//       4
//       3

Zobacz też

Dotyczy

LastIndexOf(String, String, Int32)

Wyszukuje określony podciąg i zwraca indeks zerowy ostatniego wystąpienia w sekcji ciągu źródłowego, który rozciąga się od początku ciągu do określonego indeksu.

public:
 int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex);
public:
 virtual int LastIndexOf(System::String ^ source, System::String ^ value, int startIndex);
public int LastIndexOf (string source, string value, int startIndex);
public virtual int LastIndexOf (string source, string value, int startIndex);
member this.LastIndexOf : string * string * int -> int
abstract member LastIndexOf : string * string * int -> int
override this.LastIndexOf : string * string * int -> int
Public Function LastIndexOf (source As String, value As String, startIndex As Integer) As Integer
Public Overridable Function LastIndexOf (source As String, value As String, startIndex As Integer) As Integer

Parametry

source
String

Ciąg do wyszukania.

value
String

Ciąg do zlokalizowania w obiekcie source.

startIndex
Int32

Wartość początkowa indeksu zaczynającego się od zera dla wyszukiwania wstecznego.

Zwraca

Int32

Indeks zerowy ostatniego valuewystąpienia klasy , jeśli zostanie znaleziony, w sekcji tej source rozciąga się od początku source do startIndex; w przeciwnym razie -1. Zwraca wartość startIndex , jeśli value jest znakiem, który można zignorować.

Wyjątki

source to null.

-lub-

value to null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu source.

Przykłady

Poniższy przykład określa indeksy pierwszego i ostatniego wystąpienia znaku lub podciąg w części ciągu. Zwróć uwagę, że IndexOf i LastIndexOf wyszukują w różnych fragmentach ciągu, nawet przy użyciu tego samego startIndex parametru.

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.
      array<Char>^myCharArr = gcnew array<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, gcnew 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 = "Is AE or ae the same as Æ or æ?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Æ', iS ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            æ : ", myComp->IndexOf( myStr, L'æ', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Æ', iS ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            æ : ", -1, myComp->LastIndexOf( myStr, L'æ', 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, L'Æ', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            æ : ", -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 = "Is U\u0308 or u\u0308 the same as \u00DC or \u00FC?";
   Console::WriteLine();
   myT1 = gcnew 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, L'Ü', iS ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ), -1 );
   PrintMarker( "            ü : ", myComp->IndexOf( myStr, L'ü', iS, CompareOptions::IgnoreCase ), -1 );
   myT1 = gcnew 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, L'Ü', iS ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::Ordinal ) );
   PrintMarker( "            ü : ", -1, myComp->LastIndexOf( myStr, L'ü', 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, L'Ü', iS, CompareOptions::IgnoreCase ) );
   PrintMarker( "            ü : ", -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

*/
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

*/
Imports System.Globalization

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

   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

End Class


'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

Uwagi

Ciąg źródłowy jest wyszukiwany wstecz, zaczynając od startIndex i kończąc na początku ciągu.

To przeciążenie wykonuje wyszukiwanie wrażliwe na kulturę. Wartość Unicode reprezentująca wstępnie skompilowany znak, taki jak ligatura "Æ" (U+00C6), może być uważana za równoważną każdemu wystąpieniu składników znaku w prawidłowej kolejności, takim jak "AE" (U+0041, U+0045), w zależności od kultury. Aby przeprowadzić wyszukiwanie porządkowe (niewrażliwe na kulturę), w którym porównywane są wartości Unicode, należy wywołać jedno z przeciążeń, które ma parametr typu CompareOptions i użyć CompareOptions.Ordinal wartości.

Uwaga

Jeśli to możliwe, należy wywołać metody porównania ciągów, które mają parametr typu CompareOptions , aby określić rodzaj oczekiwanego porównania. Ogólnie rzecz biorąc, użyj opcji językowych (przy użyciu bieżącej kultury) do porównywania ciągów wyświetlanych w interfejsie użytkownika i określenia CompareOptions.Ordinal lub CompareOptions.OrdinalIgnoreCase porównania zabezpieczeń.

Uwagi dotyczące wywoływania

Zestawy znaków zawierają znaki możliwe do zignorowania, które są znakami, które nie są brane pod uwagę podczas sortowania językowego lub wrażliwego na kulturę. W wyszukiwaniu uwzględniającym kulturę, jeśli value zawiera znak, który można zignorować, wynik jest odpowiednikiem wyszukiwania z usuniętym znakiem. Jeśli value składa się tylko z jednego lub większej liczby znaków LastIndexOf(String, String, Int32) , metoda zawsze zwraca startIndexwartość , która jest pozycją znaku, na której rozpoczyna się wyszukiwanie. W poniższym przykładzie LastIndexOf(String, String, Int32) metoda jest używana do znajdowania podciągów, który zawiera łącznik miękki (U+00AD) i który poprzedza lub zawiera końcowy "m" w ciągu. Ponieważ łącznik miękki w ciągu wyszukiwania jest ignorowany, wywoływanie metody w celu znalezienia podciągów składających się z łącznika miękkiego i "m" zwraca pozycję "m" w ciągu, podczas gdy wywoływanie go w celu znalezienia podciągów składających się z łącznika miękkiego i "n" zwraca pozycję "n". Gdy ciąg wyszukiwania zawiera tylko łącznik nietrwały, metoda zwraca indeks "m", który reprezentuje wartość startIndex.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

      int position = 0;

      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the index of the soft hyphen.
      position = ci.LastIndexOf(s1, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", position));

      position = ci.LastIndexOf(s2, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", position));

      // Find the index of the soft hyphen followed by "n".
      position = ci.LastIndexOf(s1, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", position));

      position = ci.LastIndexOf(s2, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", position));

      // Find the index of the soft hyphen followed by "m".
      position = ci.LastIndexOf(s1, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", position));

      position = ci.LastIndexOf(s2, "m");
      Console.WriteLine("'m' at position {0}", position);
      if (position >= 0)
         Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", position));
   }
}
// The example displays the following output:
//       'm' at position 4
//       4
//       'm' at position 3
//       3
//       'm' at position 4
//       1
//       'm' at position 3
//       1
//       'm' at position 4
//       4
//       'm' at position 3
//       3

Zobacz też

Dotyczy