在C#中,StringComparison枚舉提供了一些選項來處理特殊字符。下面是一些處理特殊字符的常用選項:
string str1 = "Café";
string str2 = "Cafe\u0301";
if (string.Equals(str1, str2, StringComparison.Ordinal))
{
Console.WriteLine("The two strings are equal using ordinal comparison.");
}
string str1 = "ABC";
string str2 = "abc";
if (string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("The two strings are equal ignoring case.");
}
string str1 = "café";
string str2 = "CAFé";
if (string.Equals(str1, str2, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("The two strings are equal using the invariant culture.");
}
string str1 = "café";
string str2 = "CAFé";
if (string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("The two strings are equal using the current culture.");
}
通過使用適當(dāng)?shù)腟tringComparison選項,您可以在C#中處理包含特殊字符的字符串。