简体   繁体   English

替换string.ToUpper()与StringComparison或类似,完全保留行为

[英]Alternative to string.ToUpper() with StringComparison or similar, that fully preserve behavior

Using left.ToUpper() == right.ToUpper() is not the best option to compare strings, at least because of Performance issues. 使用left.ToUpper() == right.ToUpper()不是比较字符串的最佳选择,至少是因为性能问题。 I want to refactor (fully preserving behavior!) this code, to something efficient, but can't achieve full equivalency for the special case. 我想重构(完全保留行为!)这段代码,有效的东西,但不能达到特殊情况的完全等效。

So, here is a simple test method: 所以,这是一个简单的测试方法:

[TestCase("Strasse", "Straße", "tr-TR")]
[TestCase("İ", "i", "tr-TR")]
public void UsingToUpper_AndCurrentCultureIgnoreCase_AreSame(string left, string right, string culture)
{
    // Arrange, Act
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
    var toUpper = left.ToUpper() == right.ToUpper();
    var stringComparison = left.Equals(right, StringComparison.CurrentCultureIgnoreCase);

    // Assert
    Assert.AreEqual(toUpper, stringComparison);
}

I tried two options, StringComparison.CurrentCultureIgnoreCase and StringComparison.OrdinalIgnoreCase both of them fails (in different cases). 我尝试了两个选项, StringComparison.CurrentCultureIgnoreCaseStringComparison.OrdinalIgnoreCase ,它们都失败了(在不同的情况下)。

So, the question: 那么,问题是:

Is there a way to compare two strings, without changing case and fully preserve the behavior of ToUpper()? 有没有办法比较两个字符串,而不改变大小写并完全保留ToUpper()的行为?

You would have to write your own custom Comparison method I am afraid. 你恐怕要编写自己的自定义比较方法。

ToUpper is making use of the Unicode metadata. ToUpper正在使用Unicode元数据。 Every character (Unicode code point) has a case as well as case mapping to upper- and lowercase (and title case). 每个字符(Unicode代码点)都有一个案例以及案例映射到大写和小写(以及标题大小写)。 .NET uses this information to convert a string to upper- or lowercase. .NET使用此信息将字符串转换为大写或小写。 You can find the very same information in the Unicode Character Database. 您可以在Unicode字符数据库中找到相同的信息。

You can supply a culture to the ToUpper method, but this would not be your goal. 您可以为ToUpper方法提供文化,但这不是您的目标。 You can write your own customCulture like defined in this answer: Create custom culture in ASP.NET 您可以编写自己的customCulture,如本答案中定义的: 在ASP.NET中创建自定义文化

However, there would not be any similar behaviour to the ToUpper method as mentioned before it uses Unicode metadata. 但是,在使用Unicode元数据之前,没有任何与ToUpper方法类似的行为。 You cannot force string Equals to use Unicode Characters. 您不能强制字符串Equals使用Unicode字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在String.ToUpper()中忽略撇号? - How To ignore apostrophe in String.ToUpper()? 使用 String.ToUpper() 时程序退出; 在一个有空格的字符串上 - Program exiting when using String.ToUpper(); on a string that has spaces in it RegEx.IsMatch()与String.ToUpper()。包含()性能 - RegEx.IsMatch() vs. String.ToUpper().Contains() performance 字符串ToUpper()函数与ToString() - string ToUpper() function with ToString() string.IndexOf(string) 的 StringComparison 方法是什么? - What is the StringComparison method of string.IndexOf(string)? CA1307:指定不为string.Equals(string)抛出StringComparison - CA1307: Specify StringComparison not thrown for string.Equals(string) 我什么时候应该使用 StringComparison.InvariantCulture 而不是 StringComparison.CurrentCulture 来测试字符串相等性? - When should I use StringComparison.InvariantCulture instead of StringComparison.CurrentCulture to test string equality? String.IndexOf(Char)vs String.IndexOf(Char,StringComparison) - String.IndexOf(Char) vs String.IndexOf(Char, StringComparison) 在.Net Standard 2.0 中替换 string.Contains(string, StringComparison) - Replacement for string.Contains(string, StringComparison) in .Net Standard 2.0 String.Starts的性能使用StringComparison.OrdinalIgnoreCase - Performance of String.StartsWith using StringComparison.OrdinalIgnoreCase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM