StringComparison Enum
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee geeft u de cultuur, hoofdletters en sorteerregels op die moeten worden gebruikt door bepaalde overbelastingen van de Compare(String, String) en Equals(Object) methoden.
public enum class StringComparison
public enum StringComparison
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum StringComparison
type StringComparison =
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type StringComparison =
Public Enum StringComparison
- Overname
- Kenmerken
Velden
| Name | Waarde | Description |
|---|---|---|
| CurrentCulture | 0 | Vergelijk tekenreeksen met behulp van cultuurgevoelige sorteerregels en de huidige cultuur. |
| CurrentCultureIgnoreCase | 1 | Vergelijk tekenreeksen met behulp van cultuurgevoelige sorteerregels, de huidige cultuur en negeer het geval van de tekenreeksen die worden vergeleken. |
| InvariantCulture | 2 | Vergelijk tekenreeksen met behulp van cultuurgevoelige sorteerregels en de invariante cultuur. |
| InvariantCultureIgnoreCase | 3 | Vergelijk tekenreeksen met behulp van cultuurgevoelige sorteerregels, de invariante cultuur en negeer het geval van de tekenreeksen die worden vergeleken. |
| Ordinal | 4 | Tekenreeksen vergelijken met behulp van ordinale (binaire) sorteerregels. |
| OrdinalIgnoreCase | 5 | Vergelijk tekenreeksen met behulp van ordinale (binaire) sorteerregels en negeer het hoofdlettergebruik van de tekenreeksen die worden vergeleken. |
Voorbeelden
In het volgende voorbeeld worden drie sets tekenreeksen vergeleken met behulp van elk lid van de StringComparison opsomming. De vergelijkingen gebruiken de conventies van de Engelse culturen (Verenigde Staten), Thai (Thailand) en Turks (Türkbytes). Houd er rekening mee dat de tekenreeksen "a" en "a-" als gelijkwaardig worden beschouwd in de "th-TH" cultuur maar niet in de andere, terwijl "i" en "İ" worden beschouwd als gelijkwaardig in de "tr-TR" cultuur wanneer hoofdletters worden genegeerd, maar niet in de andere culturen.
using System;
using System.Globalization;
using System.Threading;
public class Example3
{
public static void Main()
{
String[] cultureNames = { "en-US", "th-TH", "tr-TR" };
String[] strings1 = { "a", "i", "case", };
String[] strings2 = { "a-", "\u0130", "Case" };
StringComparison[] comparisons = (StringComparison[])Enum.GetValues(typeof(StringComparison));
foreach (var cultureName in cultureNames)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name);
for (int ctr = 0; ctr <= strings1.GetUpperBound(0); ctr++)
{
foreach (var comparison in comparisons)
Console.WriteLine(" {0} = {1} ({2}): {3}", strings1[ctr],
strings2[ctr], comparison,
String.Equals(strings1[ctr], strings2[ctr], comparison));
Console.WriteLine();
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// Current Culture: en-US
// a = a- (CurrentCulture): False
// a = a- (CurrentCultureIgnoreCase): False
// a = a- (InvariantCulture): False
// a = a- (InvariantCultureIgnoreCase): False
// a = a- (Ordinal): False
// a = a- (OrdinalIgnoreCase): False
//
// i = İ (CurrentCulture): False
// i = İ (CurrentCultureIgnoreCase): False
// i = İ (InvariantCulture): False
// i = İ (InvariantCultureIgnoreCase): False
// i = İ (Ordinal): False
// i = İ (OrdinalIgnoreCase): False
//
// case = Case (CurrentCulture): False
// case = Case (CurrentCultureIgnoreCase): True
// case = Case (InvariantCulture): False
// case = Case (InvariantCultureIgnoreCase): True
// case = Case (Ordinal): False
// case = Case (OrdinalIgnoreCase): True
//
//
// Current Culture: th-TH
// a = a- (CurrentCulture): True
// a = a- (CurrentCultureIgnoreCase): True
// a = a- (InvariantCulture): False
// a = a- (InvariantCultureIgnoreCase): False
// a = a- (Ordinal): False
// a = a- (OrdinalIgnoreCase): False
//
// i = İ (CurrentCulture): False
// i = İ (CurrentCultureIgnoreCase): False
// i = İ (InvariantCulture): False
// i = İ (InvariantCultureIgnoreCase): False
// i = İ (Ordinal): False
// i = İ (OrdinalIgnoreCase): False
//
// case = Case (CurrentCulture): False
// case = Case (CurrentCultureIgnoreCase): True
// case = Case (InvariantCulture): False
// case = Case (InvariantCultureIgnoreCase): True
// case = Case (Ordinal): False
// case = Case (OrdinalIgnoreCase): True
//
//
// Current Culture: tr-TR
// a = a- (CurrentCulture): False
// a = a- (CurrentCultureIgnoreCase): False
// a = a- (InvariantCulture): False
// a = a- (InvariantCultureIgnoreCase): False
// a = a- (Ordinal): False
// a = a- (OrdinalIgnoreCase): False
//
// i = İ (CurrentCulture): False
// i = İ (CurrentCultureIgnoreCase): True
// i = İ (InvariantCulture): False
// i = İ (InvariantCultureIgnoreCase): False
// i = İ (Ordinal): False
// i = İ (OrdinalIgnoreCase): False
//
// case = Case (CurrentCulture): False
// case = Case (CurrentCultureIgnoreCase): True
// case = Case (InvariantCulture): False
// case = Case (InvariantCultureIgnoreCase): True
// case = Case (Ordinal): False
// case = Case (OrdinalIgnoreCase): True
open System
open System.Globalization
open System.Threading
let cultureNames =
[| "en-US"; "se-SE" |]
let strings1 =
[| "case"; "encyclopædia"
"encyclopædia"; "Archæology" |]
let strings2 =
[| "Case"; "encyclopaedia"
"encyclopedia"; "ARCHÆOLOGY" |]
let comparisons =
Enum.GetValues typeof<StringComparison> :?> StringComparison[]
for cultureName in cultureNames do
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture cultureName
printfn $"Current Culture: {CultureInfo.CurrentCulture.Name}"
for i = 0 to strings1.GetUpperBound 0 do
for comparison in comparisons do
printfn $" {strings1[i]} = {strings2[i]} ({comparison}): {String.Equals(strings1[i], strings2[i], comparison)}"
printfn ""
printfn ""
// The example displays the following output:
// Current Culture: en-US
// case = Case (CurrentCulture): False
// case = Case (CurrentCultureIgnoreCase): True
// case = Case (InvariantCulture): False
// case = Case (InvariantCultureIgnoreCase): True
// case = Case (Ordinal): False
// case = Case (OrdinalIgnoreCase): True
//
// encyclopædia = encyclopaedia (CurrentCulture): True
// encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
// encyclopædia = encyclopaedia (InvariantCulture): True
// encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
// encyclopædia = encyclopaedia (Ordinal): False
// encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//
// encyclopædia = encyclopedia (CurrentCulture): False
// encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
// encyclopædia = encyclopedia (InvariantCulture): False
// encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
// encyclopædia = encyclopedia (Ordinal): False
// encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//
// Archæology = ARCHÆOLOGY (CurrentCulture): False
// Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
// Archæology = ARCHÆOLOGY (InvariantCulture): False
// Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
// Archæology = ARCHÆOLOGY (Ordinal): False
// Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
//
//
// Current Culture: se-SE
// case = Case (CurrentCulture): False
// case = Case (CurrentCultureIgnoreCase): True
// case = Case (InvariantCulture): False
// case = Case (InvariantCultureIgnoreCase): True
// case = Case (Ordinal): False
// case = Case (OrdinalIgnoreCase): True
//
// encyclopædia = encyclopaedia (CurrentCulture): False
// encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
// encyclopædia = encyclopaedia (InvariantCulture): True
// encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
// encyclopædia = encyclopaedia (Ordinal): False
// encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//
// encyclopædia = encyclopedia (CurrentCulture): False
// encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
// encyclopædia = encyclopedia (InvariantCulture): False
// encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
// encyclopædia = encyclopedia (Ordinal): False
// encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//
// Archæology = ARCHÆOLOGY (CurrentCulture): False
// Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
// Archæology = ARCHÆOLOGY (InvariantCulture): False
// Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
// Archæology = ARCHÆOLOGY (Ordinal): False
// Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Dim cultureNames() As String = { "en-US", "se-SE" }
Dim strings1() As String = { "case", "encyclopædia",
"encyclopædia", "Archæology" }
Dim strings2() As String = { "Case", "encyclopaedia",
"encyclopedia" , "ARCHÆOLOGY" }
Dim comparisons() As StringComparison = CType([Enum].GetValues(GetType(StringComparison)),
StringComparison())
For Each cultureName In cultureNames
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName)
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name)
For ctr As Integer = 0 To strings1.GetUpperBound(0)
For Each comparison In comparisons
Console.WriteLine(" {0} = {1} ({2}): {3}", strings1(ctr),
strings2(ctr), comparison,
String.Equals(strings1(ctr), strings2(ctr), comparison))
Next
Console.WriteLine()
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Current Culture: en-US
' case = Case (CurrentCulture): False
' case = Case (CurrentCultureIgnoreCase): True
' case = Case (InvariantCulture): False
' case = Case (InvariantCultureIgnoreCase): True
' case = Case (Ordinal): False
' case = Case (OrdinalIgnoreCase): True
'
' encyclopædia = encyclopaedia (CurrentCulture): True
' encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
' encyclopædia = encyclopaedia (InvariantCulture): True
' encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
' encyclopædia = encyclopaedia (Ordinal): False
' encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'
' encyclopædia = encyclopedia (CurrentCulture): False
' encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
' encyclopædia = encyclopedia (InvariantCulture): False
' encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
' encyclopædia = encyclopedia (Ordinal): False
' encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'
' Archæology = ARCHÆOLOGY (CurrentCulture): False
' Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
' Archæology = ARCHÆOLOGY (InvariantCulture): False
' Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
' Archæology = ARCHÆOLOGY (Ordinal): False
' Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
'
'
' Current Culture: se-SE
' case = Case (CurrentCulture): False
' case = Case (CurrentCultureIgnoreCase): True
' case = Case (InvariantCulture): False
' case = Case (InvariantCultureIgnoreCase): True
' case = Case (Ordinal): False
' case = Case (OrdinalIgnoreCase): True
'
' encyclopædia = encyclopaedia (CurrentCulture): False
' encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
' encyclopædia = encyclopaedia (InvariantCulture): True
' encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
' encyclopædia = encyclopaedia (Ordinal): False
' encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'
' encyclopædia = encyclopedia (CurrentCulture): False
' encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
' encyclopædia = encyclopedia (InvariantCulture): False
' encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
' encyclopædia = encyclopedia (Ordinal): False
' encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'
' Archæology = ARCHÆOLOGY (CurrentCulture): False
' Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
' Archæology = ARCHÆOLOGY (InvariantCulture): False
' Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
' Archæology = ARCHÆOLOGY (Ordinal): False
' Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
Opmerkingen
De StringComparison opsomming wordt gebruikt om op te geven of een tekenreeksvergelijking de huidige cultuur of de invariante cultuur, woord- of rangorderegels moet gebruiken en hoofdlettergevoelig of hoofdlettergevoelig moet zijn.
Important
Wanneer u een vergelijkingsmethode voor tekenreeksen aanroept, zoals String.Compare, String.Equalsof String.IndexOf, moet u altijd een overbelasting aanroepen die een parameter van het type StringComparison bevat, zodat u het type vergelijking kunt opgeven dat door de methode wordt uitgevoerd. Zie Aanbevolen procedures voor het gebruik van tekenreeksen voor meer informatie.
Een bewerking die gebruikmaakt van sorteerregels voor woorden, voert een cultuurgevoelige vergelijking uit waarbij bepaalde niet-phanumerische Unicode-tekens mogelijk speciale gewichten aan deze tekens hebben toegewezen. Met behulp van woordensorteerdregels en de conventies van een specifieke cultuur kan het afbreekstreepje (-) een zeer klein gewicht hebben, zodat 'coop' en 'co-op' naast elkaar worden weergegeven in een gesorteerde lijst.
Note
.NET Core wordt alleen uitgevoerd op Linux- en macOS-systemen: het sorteringsgedrag voor de C- en Posix-culturen is altijd hoofdlettergevoelig omdat deze culturen niet de verwachte Unicode-sorteringsvolgorde gebruiken. U wordt aangeraden een andere cultuur dan C of Posix te gebruiken voor het uitvoeren van cultuurgevoelige, hoofdletterongevoelige sorteerbewerkingen.
Een bewerking die gebruikmaakt van ordinale sorteerregels, voert een vergelijking uit op basis van de numerieke waarde (Unicode-codepunt) van elke Char waarde in de tekenreeks. Een rangtelvergelijking is snel maar cultuurgevoelig. Wanneer u rangtelregels gebruikt om tekenreeksen te sorteren die beginnen met Unicode-tekens (U+), komt de tekenreeks U+xxxx voor de tekenreeks U+jjjj als de waarde van xxxx numeriek kleiner is dan jjjj.
Zie de opmerkingen van de System.String klas voor meer informatie over vergelijkingen. Zie de opmerkingen van de System.Globalization.CultureInfo klas voor meer informatie over cultuur. Zie Aanbevolen procedures voor het gebruik van tekenreeksen voor richtlijnen voor het gebruik van ordinale of cultuurgevoelige vergelijkingsregels of de regels van de invariante cultuur. Zie Sorting Weight Tables voor een set tekstbestanden die informatie bevatten over de gewichtsgewichten die worden gebruikt bij sorteer- en vergelijkingsbewerkingen voor Windows besturingssystemen. Zie de Standaard Unicode-collatietabel voor de sorteergewichten voor Linux en macOS.