StringComparer Clase
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í.
Representa una operación de comparación de cadenas que utiliza reglas específicas basadas en la referencia cultural y el uso de mayúsculas y minúsculas o reglas específicas de comparación de ordinales.
public ref class StringComparer abstract : System::Collections::Generic::IComparer<System::String ^>, System::Collections::Generic::IEqualityComparer<System::String ^>, System::Collections::IComparer, System::Collections::IEqualityComparer
public abstract class StringComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>, System.Collections.IComparer, System.Collections.IEqualityComparer
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class StringComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>, System.Collections.IComparer, System.Collections.IEqualityComparer
type StringComparer = class
interface IComparer<string>
interface IEqualityComparer<string>
interface IComparer
interface IEqualityComparer
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type StringComparer = class
interface IComparer
interface IEqualityComparer
interface IComparer<string>
interface IEqualityComparer<string>
Public MustInherit Class StringComparer
Implements IComparer, IComparer(Of String), IEqualityComparer, IEqualityComparer(Of String)
- Herencia
-
StringComparer
- Atributos
- Implementaciones
Ejemplos
En el ejemplo siguiente se muestran las propiedades y el Create método de la StringComparer clase . En el ejemplo se muestra cómo los distintos StringComparer objetos ordenan tres versiones de la letra latina I.
// This example demonstrates members of the
// System::StringComparer class.
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Globalization;
using namespace System::Threading;
void Display(List<String^>^ stringList, String^ title)
{
Char firstChar;
int codePoint;
Console::WriteLine(title);
for each (String^ s in stringList)
{
firstChar = s[0];
codePoint = Convert::ToInt32(firstChar);
Console::WriteLine("0x{0:x}", codePoint);
}
Console::WriteLine();
}
int main()
{
// Create a list of string.
List<String^>^ stringList = gcnew List<String^>();
// Get the tr-TR (Turkish-Turkey) culture.
CultureInfo^ turkishCulture = gcnew CultureInfo("tr-TR");
// Get the culture that is associated with the current thread.
CultureInfo^ currentCulture = Thread::CurrentThread->CurrentCulture;
// Get the standard StringComparers.
StringComparer^ invariant = StringComparer::InvariantCulture;
StringComparer^ invariantIgnoreCase =
StringComparer::InvariantCultureIgnoreCase;
StringComparer^ current = StringComparer::CurrentCulture;
StringComparer^ currentIgnoreCase =
StringComparer::CurrentCultureIgnoreCase;
StringComparer^ ordinal = StringComparer::Ordinal;
StringComparer^ ordinalIgnoreCase = StringComparer::OrdinalIgnoreCase;
// Create a StringComparer that uses the Turkish culture and ignores
// case.
StringComparer^ turkishIgnoreCase =
StringComparer::Create(turkishCulture, true);
// Define three strings consisting of different versions of the
// letter I. LATIN CAPITAL LETTER I (U+0049)
String^ capitalLetterI = "I";
// LATIN SMALL LETTER I (U+0069)
String^ smallLetterI = "i";
// LATIN SMALL LETTER DOTLESS I (U+0131)
String^ smallLetterDotlessI = L"\u0131";
// Add the three strings to the list.
stringList->Add(capitalLetterI);
stringList->Add(smallLetterI);
stringList->Add(smallLetterDotlessI);
// Display the original list order.
Display(stringList, "The original order of the list entries...");
// Sort the list using the invariant culture.
stringList->Sort(invariant);
Display(stringList, "Invariant culture...");
stringList->Sort(invariantIgnoreCase);
Display(stringList, "Invariant culture, ignore case...");
// Sort the list using the current culture.
Console::WriteLine("The current culture is \"{0}\".",
currentCulture->Name);
stringList->Sort(current);
Display(stringList, "Current culture...");
stringList->Sort(currentIgnoreCase);
Display(stringList, "Current culture, ignore case...");
// Sort the list using the ordinal value of the character code points.
stringList->Sort(ordinal);
Display(stringList, "Ordinal...");
stringList->Sort(ordinalIgnoreCase);
Display(stringList, "Ordinal, ignore case...");
// Sort the list using the Turkish culture, which treats LATIN SMALL
// LETTER DOTLESS I differently than LATIN SMALL LETTER I.
stringList->Sort(turkishIgnoreCase);
Display(stringList, "Turkish culture, ignore case...");
}
/*
This code example produces the following results:
The original order of the list entries...
0x49
0x69
0x131
Invariant culture...
0x69
0x49
0x131
Invariant culture, ignore case...
0x49
0x69
0x131
The current culture is "en-US".
Current culture...
0x69
0x49
0x131
Current culture, ignore case...
0x49
0x69
0x131
Ordinal...
0x49
0x69
0x131
Ordinal, ignore case...
0x69
0x49
0x131
Turkish culture, ignore case...
0x131
0x49
0x69
*/
// This example demonstrates members of the
// System.StringComparer class.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
class Sample
{
public static void Main()
{
// Create a list of string.
List<string> list = new List<string>();
// Get the tr-TR (Turkish-Turkey) culture.
CultureInfo turkish = new CultureInfo("tr-TR");
// Get the culture that is associated with the current thread.
CultureInfo thisCulture = Thread.CurrentThread.CurrentCulture;
// Get the standard StringComparers.
StringComparer invCmp = StringComparer.InvariantCulture;
StringComparer invICCmp = StringComparer.InvariantCultureIgnoreCase;
StringComparer currCmp = StringComparer.CurrentCulture;
StringComparer currICCmp = StringComparer.CurrentCultureIgnoreCase;
StringComparer ordCmp = StringComparer.Ordinal;
StringComparer ordICCmp = StringComparer.OrdinalIgnoreCase;
// Create a StringComparer that uses the Turkish culture and ignores case.
StringComparer turkICComp = StringComparer.Create(turkish, true);
// Define three strings consisting of different versions of the letter I.
// LATIN CAPITAL LETTER I (U+0049)
string capitalLetterI = "I";
// LATIN SMALL LETTER I (U+0069)
string smallLetterI = "i";
// LATIN SMALL LETTER DOTLESS I (U+0131)
string smallLetterDotlessI = "\u0131";
// Add the three strings to the list.
list.Add(capitalLetterI);
list.Add(smallLetterI);
list.Add(smallLetterDotlessI);
// Display the original list order.
Display(list, "The original order of the list entries...");
// Sort the list using the invariant culture.
list.Sort(invCmp);
Display(list, "Invariant culture...");
list.Sort(invICCmp);
Display(list, "Invariant culture, ignore case...");
// Sort the list using the current culture.
Console.WriteLine("The current culture is \"{0}\".", thisCulture.Name);
list.Sort(currCmp);
Display(list, "Current culture...");
list.Sort(currICCmp);
Display(list, "Current culture, ignore case...");
// Sort the list using the ordinal value of the character code points.
list.Sort(ordCmp);
Display(list, "Ordinal...");
list.Sort(ordICCmp);
Display(list, "Ordinal, ignore case...");
// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER
// DOTLESS I differently than LATIN SMALL LETTER I.
list.Sort(turkICComp);
Display(list, "Turkish culture, ignore case...");
}
public static void Display(List<string> lst, string title)
{
Char c;
int codePoint;
Console.WriteLine(title);
foreach (string s in lst)
{
c = s[0];
codePoint = Convert.ToInt32(c);
Console.WriteLine("0x{0:x}", codePoint);
}
Console.WriteLine();
}
}
/*
This code example produces the following results:
The original order of the list entries...
0x49
0x69
0x131
Invariant culture...
0x69
0x49
0x131
Invariant culture, ignore case...
0x49
0x69
0x131
The current culture is "en-US".
Current culture...
0x69
0x49
0x131
Current culture, ignore case...
0x49
0x69
0x131
Ordinal...
0x49
0x69
0x131
Ordinal, ignore case...
0x69
0x49
0x131
Turkish culture, ignore case...
0x131
0x49
0x69
*/
// This example demonstrates members of the
// System.StringComparer class.
open System
open System.Globalization
open System.Threading
let display (lst: ResizeArray<string>) title =
printfn $"%s{title}"
for s in lst do
let c = s[0]
let codePoint = Convert.ToInt32 c
printfn $"0x{codePoint:x}"
printfn ""
// Create a list of string.
let list = ResizeArray()
// Get the tr-TR (Turkish-Turkey) culture.
let turkish = CultureInfo "tr-TR"
// Get the culture that is associated with the current thread.
let thisCulture = Thread.CurrentThread.CurrentCulture
// Get the standard StringComparers.
let invCmp = StringComparer.InvariantCulture
let invICCmp = StringComparer.InvariantCultureIgnoreCase
let currCmp = StringComparer.CurrentCulture
let currICCmp = StringComparer.CurrentCultureIgnoreCase
let ordCmp = StringComparer.Ordinal
let ordICCmp = StringComparer.OrdinalIgnoreCase
// Create a StringComparer that uses the Turkish culture and ignores case.
let turkICComp = StringComparer.Create(turkish, true)
// Define three strings consisting of different versions of the letter I.
// LATIN CAPITAL LETTER I (U+0049)
let capitalLetterI = "I"
// LATIN SMALL LETTER I (U+0069)
let smallLetterI = "i"
// LATIN SMALL LETTER DOTLESS I (U+0131)
let smallLetterDotlessI = "\u0131"
// Add the three strings to the list.
list.Add capitalLetterI
list.Add smallLetterI
list.Add smallLetterDotlessI
// Display the original list order.
display list "The original order of the list entries..."
// Sort the list using the invariant culture.
list.Sort invCmp
display list "Invariant culture..."
list.Sort invICCmp
display list "Invariant culture, ignore case..."
// Sort the list using the current culture.
printfn $"The current culture is \"{thisCulture.Name}\"."
list.Sort currCmp
display list "Current culture..."
list.Sort currICCmp
display list "Current culture, ignore case..."
// Sort the list using the ordinal value of the character code points.
list.Sort ordCmp
display list "Ordinal..."
list.Sort ordICCmp
display list "Ordinal, ignore case..."
// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER
// DOTLESS I differently than LATIN SMALL LETTER I.
list.Sort turkICComp
display list "Turkish culture, ignore case..."
(*
This code example produces the following results:
The original order of the list entries...
0x49
0x69
0x131
Invariant culture...
0x69
0x49
0x131
Invariant culture, ignore case...
0x49
0x69
0x131
The current culture is "en-US".
Current culture...
0x69
0x49
0x131
Current culture, ignore case...
0x49
0x69
0x131
Ordinal...
0x49
0x69
0x131
Ordinal, ignore case...
0x69
0x49
0x131
Turkish culture, ignore case...
0x131
0x49
0x69
*)
' This code example demonstrates members of the System.StringComparer class.
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading
Class Sample
Public Shared Sub Main()
' Create a list of string.
Dim list As New List(Of String)
' Get the tr-TR (Turkish-Turkey) culture.
Dim turkish As New CultureInfo("tr-TR")
' Get the culture that is associated with the current thread.
Dim thisCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
' Get the standard StringComparers.
Dim invCmp As StringComparer = StringComparer.InvariantCulture
Dim invICCmp As StringComparer = StringComparer.InvariantCultureIgnoreCase
Dim currCmp As StringComparer = StringComparer.CurrentCulture
Dim currICCmp As StringComparer = StringComparer.CurrentCultureIgnoreCase
Dim ordCmp As StringComparer = StringComparer.Ordinal
Dim ordICCmp As StringComparer = StringComparer.OrdinalIgnoreCase
' Create a StringComparer that uses the Turkish culture and ignores case.
Dim turkICComp As StringComparer = StringComparer.Create(turkish, True)
' Define three strings consisting of different versions of the letter I.
' LATIN CAPITAL LETTER I (U+0049)
Dim capitalLetterI As String = "I"
' LATIN SMALL LETTER I (U+0069)
Dim smallLetterI As String = "i"
' LATIN SMALL LETTER DOTLESS I (U+0131)
Dim smallLetterDotlessI As String = "ı"
' Add the three strings to the list.
list.Add(capitalLetterI)
list.Add(smallLetterI)
list.Add(smallLetterDotlessI)
' Display the original list order.
Display(list, "The original order of the list entries...")
' Sort the list using the invariant culture.
list.Sort(invCmp)
Display(list, "Invariant culture...")
list.Sort(invICCmp)
Display(list, "Invariant culture, ignore case...")
' Sort the list using the current culture.
Console.WriteLine("The current culture is ""{0}"".", thisCulture.Name)
list.Sort(currCmp)
Display(list, "Current culture...")
list.Sort(currICCmp)
Display(list, "Current culture, ignore case...")
' Sort the list using the ordinal value of the character code points.
list.Sort(ordCmp)
Display(list, "Ordinal...")
list.Sort(ordICCmp)
Display(list, "Ordinal, ignore case...")
' Sort the list using the Turkish culture, which treats LATIN SMALL LETTER
' DOTLESS I differently than LATIN SMALL LETTER I.
list.Sort(turkICComp)
Display(list, "Turkish culture, ignore case...")
End Sub
Public Shared Sub Display(ByVal lst As List(Of String), ByVal title As String)
Dim c As Char
Dim s As String
Dim codePoint As Integer
Console.WriteLine(title)
For Each s In lst
c = s(0)
codePoint = Convert.ToInt32(c)
Console.WriteLine("0x{0:x}", codePoint)
Next s
Console.WriteLine()
End Sub
End Class
'This code example produces the following results:
'
'The original order of the list entries...
'0x49
'0x69
'0x131
'
'Invariant culture...
'0x69
'0x49
'0x131
'
'Invariant culture, ignore case...
'0x49
'0x69
'0x131
'
'The current culture is "en-US".
'Current culture...
'0x69
'0x49
'0x131
'
'Current culture, ignore case...
'0x49
'0x69
'0x131
'
'Ordinal...
'0x49
'0x69
'0x131
'
'Ordinal, ignore case...
'0x69
'0x49
'0x131
'
'Turkish culture, ignore case...
'0x131
'0x49
'0x69
'
Comentarios
Para obtener más información sobre esta API, consulte Comentarios complementarios de api para StringComparer.
Constructores
StringComparer() |
Inicializa una nueva instancia de la clase StringComparer. |
Propiedades
CurrentCulture |
Obtiene un objeto StringComparer que realiza una comparación de cadenas con distinción entre mayúsculas y minúsculas usando las reglas de comparación de palabras de la referencia cultural actual. |
CurrentCultureIgnoreCase |
Obtiene un objeto StringComparer que realiza comparaciones de cadenas sin distinción entre mayúsculas y minúsculas usando las reglas de comparación de palabras de la referencia cultural actual. |
InvariantCulture |
Obtiene un objeto StringComparer que realiza una comparación de cadenas con distinción entre mayúsculas y minúsculas utilizando las reglas de comparación de palabras de la referencia cultural de todos los idiomas. |
InvariantCultureIgnoreCase |
Obtiene un objeto StringComparer que realiza una comparación de cadenas sin distinción entre mayúsculas y minúsculas utilizando las reglas de comparación de palabras de la referencia cultural de todos los idiomas. |
Ordinal |
Obtiene un objeto StringComparer que realiza una comparación de cadenas ordinales donde se hace distinción entre mayúsculas y minúsculas. |
OrdinalIgnoreCase |
Obtiene un objeto StringComparer que realiza una comparación de cadenas ordinales donde no se hace distinción entre mayúsculas y minúsculas. |
Métodos
Compare(Object, Object) |
Cuando se reemplaza en una clase derivada, compara dos objetos y devuelve una indicación de su criterio de ordenación relativo. |
Compare(String, String) |
Cuando se reemplaza en una clase derivada, compara dos cadenas y devuelve una indicación de su criterio de ordenación relativo. |
Create(CultureInfo, Boolean) |
Crea un objeto StringComparer que compara cadenas según las reglas de la referencia cultural especificada. |
Create(CultureInfo, CompareOptions) |
Crea un objeto StringComparer que compara cadenas según las reglas de las opciones de referencia cultural y cadena especificadas. |
Equals(Object) |
Determina si el objeto especificado es igual que el objeto actual. (Heredado de Object) |
Equals(Object, Object) |
Cuando se reemplaza en una clase derivada, indica si dos objetos son iguales. |
Equals(String, String) |
Cuando se reemplaza en una clase derivada, indica si dos cadenas son iguales. |
FromComparison(StringComparison) |
Convierte la instancia StringComparison especificada en una instancia StringComparer. |
GetHashCode() |
Sirve como la función hash predeterminada. (Heredado de Object) |
GetHashCode(Object) |
Cuando se reemplaza en una clase derivada, obtiene el código hash del objeto especificado. |
GetHashCode(String) |
Cuando se reemplaza en una clase derivada, obtiene el código hash de la cadena especificada. |
GetType() |
Obtiene el Type de la instancia actual. (Heredado de Object) |
IsWellKnownCultureAwareComparer(IEqualityComparer<String>, CompareInfo, CompareOptions) |
Determina si el especificado IEqualityComparer<T> es un comparador de cadenas conocido que tiene en cuenta la referencia cultural. |
IsWellKnownOrdinalComparer(IEqualityComparer<String>, Boolean) |
Determina si el especificado IEqualityComparer<T> es un comparador de cadenas ordinal conocido. |
MemberwiseClone() |
Crea una copia superficial del Object actual. (Heredado de Object) |
ToString() |
Devuelve una cadena que representa el objeto actual. (Heredado de Object) |
Implementaciones de interfaz explícitas
IComparer.Compare(Object, Object) |
Compara dos objetos y devuelve un valor que indica si uno de ellos es menor, igual o mayor que el otro. |
IEqualityComparer.Equals(Object, Object) |
Determina si los objetos especificados son iguales. |
IEqualityComparer.GetHashCode(Object) |
Devuelve un código hash para el objeto especificado. |