CompareInfo.LastIndexOf Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Devuelve el índice basado en cero de la última aparición de un valor en una cadena o en una parte de ella.
Sobrecargas
LastIndexOf(String, String, Int32, Int32, CompareOptions) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado, utilizando el valor de CompareOptions especificado. |
LastIndexOf(String, Char, Int32, Int32, CompareOptions) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado, utilizando el valor de CompareOptions especificado. |
LastIndexOf(String, String, Int32, Int32) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado. |
LastIndexOf(String, String, Int32, CompareOptions) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el principio de la cadena hasta el índice especificado, utilizando el valor de CompareOptions especificado. |
LastIndexOf(String, Char, Int32, Int32) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado. |
LastIndexOf(String, Char, Int32, CompareOptions) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el principio de la cadena hasta el índice especificado, utilizando el valor de CompareOptions especificado. |
LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32) |
Busca la última aparición de una subcadena dentro de una cadena de origen. |
LastIndexOf(String, Char) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen. |
LastIndexOf(String, String, CompareOptions) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen utilizando el valor de CompareOptions especificado. |
LastIndexOf(String, Char, Int32) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el comienzo de la cadena hasta el índice especificado. |
LastIndexOf(String, Char, CompareOptions) |
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen utilizando el valor de CompareOptions especificado. |
LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions) |
Busca la última aparición de un objeto Rune en el intervalo de caracteres de solo lectura especificado. |
LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions) |
Busca la última aparición de una subcadena dentro del intervalo de caracteres de solo lectura especificado. |
LastIndexOf(String, String) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen. |
LastIndexOf(String, String, Int32) |
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el comienzo de la cadena hasta el índice especificado. |
LastIndexOf(String, String, Int32, Int32, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado, utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- count
- Int32
Número de elementos de la sección en la que se va a realizar la búsqueda.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que contiene el número de elementos especificados por count
y termina en startIndex
, usando las opciones de comparación especificadas; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
startIndex
está fuera del intervalo de índices válidos para la source
.
O bien
count
es menor que cero.
O bien
startIndex
y count
no especifican una sección válida en source
.
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina en startIndex
- count
+ 1.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue referencias culturales. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor , esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural (es decir, si options
no es Ordinal o OrdinalIgnoreCase), si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String, Int32, Int32, CompareOptions) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda.
En el ejemplo siguiente, el LastIndexOf(String, String, Int32, Int32, CompareOptions) método se usa para buscar la posición de un guión suave (U+00AD) seguido de un "m" en todo, pero la primera posición del carácter anterior a la final "m" en dos cadenas. Solo una de las cadenas contiene la subcadena necesaria. En ambos casos, como el guion virtual es un carácter que se puede pasar por alto, el método devuelve el índice de "m" en la cadena cuando realiza una comparación que tiene en cuenta la referencia cultural. Sin embargo, cuando realiza una comparación ordinal, encuentra la subcadena solo en la primera cadena. Tenga en cuenta que, en el caso de la primera cadena, que incluye el guion flexible seguido de "m", el método no devuelve el índice del guión flexible, sino que devuelve el índice de la "m" cuando realiza una comparación que distingue la referencia cultural. El método devuelve el índice del guion virtual de la primera cadena solo cuando realiza una comparación ordinal.
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
Consulte también
Se aplica a
LastIndexOf(String, Char, Int32, Int32, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado, utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- count
- Int32
Número de elementos de la sección en la que se va a realizar la búsqueda.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que contiene el número de elementos especificados por count
y termina en startIndex
, usando las opciones de comparación especificadas; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
source
es null
.
startIndex
está fuera del intervalo de índices válidos para la source
.
O bien
count
es menor que cero.
O bien
startIndex
y count
no especifican una sección válida en source
.
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina en startIndex
- count
+ 1.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue referencias culturales. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor , esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural). Un carácter se considera equivalente a otro carácter solo si los valores Unicode son los mismos. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tenga en cuenta la referencia cultural, si value
es un carácter ignorable, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char, Int32, Int32, CompareOptions) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda. En el ejemplo siguiente, el LastIndexOf(String, Char, Int32, Int32, CompareOptions) método se usa para buscar un guion suave (U+00AD) que precede al final "m" en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guión flexible es un carácter que se puede pasar por alto, una búsqueda que distingue la referencia cultural devuelve la posición de índice de "m", que es el valor de startIndex
. Sin embargo, una búsqueda ordinal encuentra correctamente el guión suave en una cadena e informa de que no está presente en la segunda cadena.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As Char = ChrW(&h00AD)
Dim position As Integer
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen using culture-sensitive comparison.
position = ci.LastIndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position, _
position + 1, CompareOptions.IgnoreCase))
End If
position = ci.LastIndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position, _
position + 1, CompareOptions.IgnoreCase))
End If
' Find the index of the soft hyphen using ordinal comparison.
position = ci.LastIndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position, _
position + 1, CompareOptions.Ordinal))
End If
position = ci.LastIndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position, _
position + 1, CompareOptions.Ordinal))
End If
End Sub
End Module
' 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
Consulte también
Se aplica a
LastIndexOf(String, String, Int32, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- count
- Int32
Número de elementos de la sección en la que se va a realizar la búsqueda.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que contiene el número de elementos especificado por count
y que termina en startIndex
; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
startIndex
está fuera del intervalo de índices válidos para la source
.
O bien
count
es menor que cero.
O bien
startIndex
y count
no especifican una sección válida en source
.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina en startIndex
- count
+ 1.
Esta sobrecarga realiza una búsqueda que distingue referencias culturales. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el Ordinal valor.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad .
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural, si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String, Int32, Int32) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda.
En el ejemplo siguiente, el LastIndexOf(String, String, Int32, Int32) método se usa para buscar la posición de un guion suave (U+00AD) seguido de un "m" en los tres caracteres que preceden a la "m" final de dos cadenas. Solo una de las cadenas contiene la subcadena necesaria. En ambos casos, como el guion virtual es un carácter que se puede pasar por alto, el método devuelve el índice de "m" en la cadena cuando realiza una comparación que tiene en cuenta la referencia cultural. Observe que en el caso de la primera cadena, que incluye el guion virtual seguido de una "m", el método no devuelve el índice del guion virtual, sino el índice de "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
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) + "mal"
Dim s2 As String = "animal"
Dim position As Integer
position = ci.LastIndexOf(s1, "m"c)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, searchString, position, 3, CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s1, searchString, position, 3, CompareOptions.Ordinal))
End If
position = ci.LastIndexOf(s2, "m"c)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, searchString, position, 3, CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s2, searchString, position, 3, CompareOptions.Ordinal))
End If
End Sub
End Module
' The example displays the following output:
' 4
' 3
' 3
' -1
Consulte también
Se aplica a
LastIndexOf(String, String, Int32, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el principio de la cadena hasta el índice especificado, utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que abarca desde el principio de source
hasta startIndex
, usando las opciones de comparación especificadas; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
startIndex
está fuera del intervalo de índices válidos para la source
.
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena. Tenga en cuenta que IndexOf y LastIndexOf buscan en diferentes partes de la cadena, incluso con el mismo startIndex
parámetro.
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
Comentarios
La cadena de origen se busca hacia atrás empezando por startIndex
y finalizando al principio de la cadena.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue referencias culturales. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor , esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural (es decir, si options
no es Ordinal o OrdinalIgnoreCase), si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String, Int32, CompareOptions) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda.
En el ejemplo siguiente, el LastIndexOf(String, String, Int32, CompareOptions) método se usa para buscar la posición de un guión suave (U+00AD) seguido de un "m", empezando por el final "m" en dos cadenas. Solo una de las cadenas contiene la subcadena necesaria. En ambos casos, como el guion virtual es un carácter que se puede pasar por alto, el método devuelve el índice de "m" en la cadena cuando realiza una comparación que tiene en cuenta la referencia cultural. Observe que en el caso de la primera cadena, que incluye el guion virtual seguido de una "m", el método no devuelve el índice del guion virtual, sino el índice de "m". El método devuelve el índice del guion virtual de la primera cadena solo cuando realiza una comparación ordinal.
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
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) + "mal"
Dim s2 As String = "animal"
Dim position As Integer
position = ci.LastIndexOf(s1, "m"c)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.Ordinal))
End If
position = ci.LastIndexOf(s2, "m"c)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.Ordinal))
End If
End Sub
End Module
' The example displays the following output:
' 4
' 3
' 3
' -1
Consulte también
Se aplica a
LastIndexOf(String, Char, Int32, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que contiene el número de elementos especificado y termina en el índice especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- count
- Int32
Número de elementos de la sección en la que se va a realizar la búsqueda.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que contiene el número de elementos especificado por count
y que termina en startIndex
; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
source
es null
.
startIndex
está fuera del intervalo de índices válidos para la source
.
O bien
count
es menor que cero.
O bien
startIndex
y count
no especifican una sección válida en source
.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina en startIndex
- count
+ 1.
Esta sobrecarga realiza una búsqueda que distingue referencias culturales. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores Unicode son los mismos, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el Ordinal valor. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tenga en cuenta la referencia cultural, si value
es un carácter ignorable, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char, Int32, Int32) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda. En el ejemplo siguiente, el LastIndexOf(String, Char, Int32, Int32) método se usa para buscar un guion suave (U+00AD) que precede al final "m" en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guion flexible es un carácter que se puede pasar por alto, el método devuelve la posición de índice de "m", que es el valor de 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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim position As Integer
Dim softHyphen As Char = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen.
position = ci.IndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position, position + 1))
End If
position = ci.IndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position, position + 1))
End If
End Sub
End Module
' The example displays the following output:
' 'm' at position 4
' 4
' 'm' at position 3
' 3
Consulte también
Se aplica a
LastIndexOf(String, Char, Int32, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el principio de la cadena hasta el índice especificado, utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que abarca desde el principio de source
hasta startIndex
, usando las opciones de comparación especificadas; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
source
es null
.
startIndex
está fuera del intervalo de índices válidos para la source
.
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena. Tenga en cuenta que IndexOf y LastIndexOf buscan en diferentes partes de la cadena, incluso con el mismo startIndex
parámetro.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina al principio de la cadena.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor, esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural). Un carácter se considera equivalente a otro carácter solo si los valores Unicode son iguales. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres que se pueden omitir, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o que distingue referencia cultural. En una búsqueda que tenga en cuenta la referencia cultural, si value
es un carácter ignorable, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char, Int32, CompareOptions) método siempre devuelve startIndex
, que es la posición de carácter en la que comienza la búsqueda. En el ejemplo siguiente, el LastIndexOf(String, Char, Int32, CompareOptions) método se usa para buscar un guion suave (U+00AD) que precede al final "m" en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guion flexible es un carácter que se puede omitir, una búsqueda que distingue referencias culturales devuelve la posición de índice de "m". Sin embargo, una búsqueda ordinal encuentra correctamente el guión flexible en una cadena e informa de que no está presente en la segunda cadena.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As Char = ChrW(&h00AD)
Dim position As Integer
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen using culture-sensitive comparison.
position = ci.LastIndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position, CompareOptions.IgnoreCase))
End If
position = ci.LastIndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position, CompareOptions.IgnoreCase))
End If
' Find the index of the soft hyphen using ordinal comparison.
position = ci.LastIndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position, CompareOptions.Ordinal))
End If
position = ci.LastIndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position, CompareOptions.Ordinal))
End If
End Sub
End Module
' 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
Consulte también
Se aplica a
LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la última aparición de una subcadena dentro de una cadena de origen.
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
Parámetros
- source
- ReadOnlySpan<Char>
Intervalo de caracteres de solo lectura en el que se realizará la búsqueda.
- value
- ReadOnlySpan<Char>
El intervalo de solo lectura de caracteres que contiene la subcadena que se va a buscar dentro de source
.
- options
- CompareOptions
Propiedad CompareOptions que se va a usar durante la búsqueda.
- matchLength
- Int32
Cuando este método devuelve resultados, contiene el número de caracteres de source
que coincidieron con el valor deseado. Puede ser distinto de la longitud de value
si se realiza una comparación lingüística. Se establece en 0 si value
no se encuentra dentro de source
.
Devoluciones
Índice de base cero en source
donde aparece la subcadena value
en último lugar; o bien un número negativo si no se puede encontrar value
en source
.
Excepciones
options
contiene una combinación no admitida de marcas.
Comentarios
Este método tiene una sobrecarga mayor que otras LastIndexOf sobrecargas que no toman un matchLength
argumento. Llame a esta sobrecarga solo si necesita la información de longitud de coincidencia.
Se aplica a
LastIndexOf(String, Char)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, dentro de source
; en caso contrario, -1.
Excepciones
source
es null
.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir del final de la cadena y termina al principio de la cadena.
Esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores Unicode son iguales, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el Ordinal valor. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda sensible a la referencia cultural, si value
es un carácter que se puede ignorar, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char) método siempre devuelve la última posición de índice en source
para indicar que la coincidencia se encuentra al final de source
. En el ejemplo siguiente, el LastIndexOf(String, Char) método se usa para buscar el guion suave (U+00AD) en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guion flexible es un carácter que se puede ignorar, el método devuelve la última posición de índice de la cadena para indicar que ha encontrado una coincidencia al final de la cadena.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As Char = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the last soft hyphen.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen))
End Sub
End Module
' The example displays the following output:
' 6
' 5
Consulte también
Se aplica a
LastIndexOf(String, String, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, dentro de source
, usando las opciones de comparación especificadas; en caso contrario, -1.
Excepciones
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir del final de la cadena y termina al principio de la cadena.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor , esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural (es decir, si options
no es Ordinal o OrdinalIgnoreCase), si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String, CompareOptions) método siempre devuelve source
.Length - 1, que representa la última posición de índice en source
. En el ejemplo siguiente, el LastIndexOf(String, String, CompareOptions) método se usa para buscar tres subcadenas (un guión suave (U+00AD), un guión suave seguido de "n" y un guión suave seguido de "m") en dos cadenas. Solo una de las cadenas contiene un guion virtual. Dado que el guión suave es un carácter que se puede ignorar, una búsqueda que distingue la referencia cultural devuelve el mismo valor que devolvería si el guión suave no se incluyera en la cadena de búsqueda. Sin embargo, una búsqueda ordinal encuentra correctamente el guión flexible en una cadena e informa de que no está presente en la segunda cadena.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
Console.WriteLine("Culture-sensitive comparison:")
' Use culture-sensitive comparison to find the last soft hyphen.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, CompareOptions.None))
' Use culture-sensitive comparison to find the last soft hyphen followed by "n".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "n", CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "n", CompareOptions.None))
' Use culture-sensitive comparison to find the last soft hyphen followed by "m".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "m", CompareOptions.None))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "m", CompareOptions.None))
Console.WriteLine("Ordinal comparison:")
' Use ordinal comparison to find the last soft hyphen.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, CompareOptions.Ordinal))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, CompareOptions.Ordinal))
' Use ordinal comparison to find the last soft hyphen followed by "n".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "n", CompareOptions.Ordinal))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "n", CompareOptions.Ordinal))
' Use ordinal comparison to find the last soft hyphen followed by "m".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "m", CompareOptions.Ordinal))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "m", CompareOptions.Ordinal))
End Sub
End Module
' The example displays the following output:
' 6
' 5
' 1
' 1
' 4
' 3
' Ordinal comparison:
' 3
' -1
' -1
' -1
' 3
' -1
Consulte también
Se aplica a
LastIndexOf(String, Char, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el comienzo de la cadena hasta el índice especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que abarca desde el principio de source
hasta startIndex
; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
source
es null
.
startIndex
está fuera del intervalo de índices válidos para la source
.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena. Tenga en cuenta que IndexOf y LastIndexOf buscan en diferentes partes de la cadena, incluso con el mismo startIndex
parámetro.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina al principio de la cadena.
Esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde un carácter se considera equivalente a otro carácter solo si los valores Unicode son iguales, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el Ordinal valor. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda sensible a la referencia cultural, si value
es un carácter que se puede ignorar, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char, Int32) método siempre devuelve startIndex
, que es la posición del carácter en la que comienza la búsqueda. En el ejemplo siguiente, el LastIndexOf(String, Char, Int32) método se usa para buscar un guión suave (U+00AD) que precede a la "m" final en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guion flexible es un carácter que se puede ignorar, el método devuelve la posición de índice de "m", que es el valor de 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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim position As Integer
Dim softHyphen As Char = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the last index of the soft hyphen.
position = ci.LastIndexOf(s1, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position))
End If
position = ci.LastIndexOf(s2, "m"c)
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position))
End If
End Sub
End Module
' The example displays the following output:
' 'm' at position 4
' 4
' 'm' at position 3
' 3
Consulte también
Se aplica a
LastIndexOf(String, Char, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca el carácter especificado y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen utilizando el valor de CompareOptions especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- Char
Carácter que se va a buscar en source
.
- options
- CompareOptions
Valor que define cómo deben compararse los parámetros source
y value
. options
es el valor de la enumeración Ordinal usado de forma aislada, o una combinación bit a bit de uno o varios de los valores siguientes: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth y IgnoreKanaType.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, dentro de source
, usando las opciones de comparación especificadas; en caso contrario, -1.
Excepciones
source
es null
.
options
contiene un valor de CompareOptions no válido.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir del final de la cadena y termina al principio de la cadena.
El CompareOptions.StringSort valor no es válido para este método.
Si options
no incluye el Ordinal valor , esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Si el carácter es un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), puede considerarse equivalente a cualquier aparición de sus componentes en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Si options
incluye el Ordinal valor, esta sobrecarga realiza una búsqueda ordinal (que no distingue la referencia cultural). Un carácter se considera equivalente a otro carácter solo si los valores Unicode son iguales. Las sobrecargas de String.LastIndexOf que buscan un carácter realizan una búsqueda ordinal, mientras que las que buscan una cadena realizan una búsqueda que distingue la referencia cultural.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda sensible a la referencia cultural, si value
es un carácter que se puede ignorar, el resultado es equivalente a buscar con ese carácter quitado. En este caso, el LastIndexOf(String, Char, CompareOptions) método siempre devuelve la última posición de carácter en source
para indicar que la coincidencia se encuentra al final de source
. En el ejemplo siguiente, el LastIndexOf(String, Char, CompareOptions) método se usa para buscar un guion suave (U+00AD) en dos cadenas. Solo una de las cadenas contiene un guion virtual. En ambos casos, dado que el guión temporal es un carácter que se puede ignorar, una búsqueda que distingue la referencia cultural devuelve la última posición de índice de la cadena de origen. Por el contrario, una búsqueda ordinal encuentra correctamente el guión flexible en una cadena e informa de que está ausente de la segunda cadena.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As Char = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the last index of the soft hyphen using culture-sensitive comparison.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, CompareOptions.IgnoreCase))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, CompareOptions.IgnoreCase))
' Find the last index of the soft hyphen using ordinal comparison.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, CompareOptions.Ordinal))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, CompareOptions.Ordinal))
End Sub
End Module
' The example displays the following output:
' 6
' 5
' 3
' -1
Consulte también
Se aplica a
LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la última aparición de un objeto Rune en el intervalo de caracteres de solo lectura especificado.
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
Parámetros
- source
- ReadOnlySpan<Char>
Intervalo de caracteres de solo lectura en el que se realizará la búsqueda.
- options
- CompareOptions
Combinación opcional de los valores de enumeración de CompareOptions que se va a usar durante la búsqueda. El valor predeterminado es None.
Devoluciones
Índice de base cero en source
donde aparece value
en último lugar; o bien un valor negativo si no se puede encontrar value
en source
.
Excepciones
options
contiene una combinación no admitida de marcas.
Se aplica a
LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la última aparición de una subcadena dentro del intervalo de caracteres de solo lectura especificado.
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
Parámetros
- source
- ReadOnlySpan<Char>
Intervalo de caracteres de solo lectura en el que se realizará la búsqueda.
- value
- ReadOnlySpan<Char>
Subcadena que se buscará en source
.
- options
- CompareOptions
Combinación opcional de los valores de enumeración de CompareOptions que se va a usar durante la búsqueda. El valor predeterminado es None.
Devoluciones
Índice de base cero en source
donde aparece la subcadena value
en último lugar; o bien un valor negativo si no se puede encontrar value
en source
.
Excepciones
options
contiene una combinación no admitida de marcas.
Se aplica a
LastIndexOf(String, String)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en toda la cadena de origen.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, dentro de source
; en caso contrario, -1.
Excepciones
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una cadena.
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
Comentarios
La cadena de origen se busca hacia atrás a partir del final de la cadena y termina al principio de la cadena.
Esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el CompareOptions.Ordinal valor.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural, si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String) método siempre devuelve source
.Length - 1, que representa la última posición de índice en source
. En el ejemplo siguiente, el LastIndexOf(String, String) método se usa para buscar tres subcadenas (un guión suave (U+00AD), un guión suave seguido de "n" y un guión suave seguido de "m") en dos cadenas. Solo una de las cadenas contiene un guion virtual. En cada caso, dado que el guión suave es un carácter ignorable, el resultado es el mismo que si el guión suave no se hubiera incluido en value
. Al buscar solo un guión suave, el método devuelve 6 y 5. Estos valores corresponden al índice del último carácter de las dos cadenas.
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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the last soft hyphen.
Console.WriteLine(ci.LastIndexOf(s1, softHyphen))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen))
' Find the index of the last soft hyphen followed by "n".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "n"))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "n"))
' Find the index of the last soft hyphen followed by "m".
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "m"))
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "m"))
End Sub
End Module
' The example displays the following output:
' 6
' 5
' 1
' 1
' 4
' 3
Consulte también
Se aplica a
LastIndexOf(String, String, Int32)
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
- Source:
- CompareInfo.cs
Busca la subcadena especificada y devuelve el índice de base cero de la última aparición incluida en la sección de la cadena de origen que abarca desde el comienzo de la cadena hasta el índice especificado.
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
Parámetros
- source
- String
Cadena en la que se va a buscar.
- value
- String
Cadena que se va a buscar en source
.
- startIndex
- Int32
Índice inicial de base cero de la búsqueda hacia atrás.
Devoluciones
Índice de base cero de la última aparición de value
, si se encuentra, en la sección de source
que abarca desde el principio de source
hasta startIndex
; en caso contrario, -1. Devuelve startIndex
si value
es un carácter ignorable.
Excepciones
startIndex
está fuera del intervalo de índices válidos para la source
.
Ejemplos
En el ejemplo siguiente se determinan los índices de las primeras y últimas apariciones de un carácter o una subcadena dentro de una parte de una cadena. Tenga en cuenta que IndexOf y LastIndexOf buscan en diferentes partes de la cadena, incluso con el mismo startIndex
parámetro.
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
Comentarios
La cadena de origen se busca hacia atrás a partir startIndex
de y termina al principio de la cadena.
Esta sobrecarga realiza una búsqueda que distingue la referencia cultural. Un valor Unicode que representa un carácter precomponido, como la ligadura "Æ" (U+00C6), podría considerarse equivalente a cualquier aparición de los componentes del carácter en la secuencia correcta, como "AE" (U+0041, U+0045), dependiendo de la referencia cultural. Para realizar una búsqueda ordinal (que no distingue la referencia cultural), donde se comparan los valores Unicode, debe llamar a una de las sobrecargas que tienen un parámetro de tipo CompareOptions y usar el CompareOptions.Ordinal valor.
Nota
Cuando sea posible, debe llamar a métodos de comparación de cadenas que tengan un parámetro de tipo CompareOptions para especificar el tipo de comparación esperado. Como regla general, use opciones lingüísticas (mediante la referencia cultural actual) para comparar cadenas mostradas en la interfaz de usuario y especificar CompareOptions.Ordinal o CompareOptions.OrdinalIgnoreCase para comparaciones de seguridad.
Notas a los autores de las llamadas
Los juegos de caracteres incluyen caracteres ignorables, que son caracteres que no se tienen en cuenta al realizar una ordenación lingüística o sensible a la referencia cultural. En una búsqueda que tiene en cuenta la referencia cultural, si value
contiene un carácter que se puede pasar por alto, el resultado es equivalente a buscar sin ese carácter. Si value
solo consta de uno o varios caracteres ignorables, el LastIndexOf(String, String, Int32) método siempre devuelve startIndex
, que es la posición del carácter en la que comienza la búsqueda. En el ejemplo siguiente, el LastIndexOf(String, String, Int32) método se usa para buscar una subcadena que incluya un guión suave (U+00AD) y que precede o incluye la "m" final en una cadena. Dado que se omite el guión suave de la cadena de búsqueda, al llamar al método para buscar una subcadena que consta del guión suave y "m" devuelve la posición de "m" en la cadena, mientras que llamarla para buscar una subcadena que consta del guión suave y "n" devuelve la posición de "n". Cuando la cadena de búsqueda contiene solo el guión temporal, el método devuelve el índice de "m", que representa el valor de 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
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
Dim position As Integer
Dim softHyphen As String = ChrW(&h00AD)
Dim s1 As String = "ani" + softHyphen + "mal"
Dim s2 As String = "animal"
' Find the index of the soft hyphen.
position = ci.LastIndexOf(s1, "m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen, position))
End If
position = ci.LastIndexOf(s2, "m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen, position))
End If
' 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 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "n", position))
End If
position = ci.LastIndexOf(s2, "m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "n", position))
End If
' 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 Then
Console.WriteLine(ci.LastIndexOf(s1, softHyphen + "m", position))
End If
position = ci.LastIndexOf(s2, "m")
Console.WriteLine("'m' at position {0}", position)
If position >= 0 Then
Console.WriteLine(ci.LastIndexOf(s2, softHyphen + "m", position))
End If
End Sub
End Module
' 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