简体   繁体   English

string.Compare(String, Int32, String, Int32, Int32) 在 .NET 5.0 中未正确比较泰国文化

[英]string.Compare(String, Int32, String, Int32, Int32) is not compared correctly for Thai Culture in .NET 5.0

In the code snippet below.在下面的代码片段中。 In the case of Thai Culture, the result should be -1 , but it is 0 .在 Thai Culture 的情况下,结果应该是-1 ,但它是0

Is anyone aware of the reason for this?有人知道这是为什么吗?

public void CountryCulture()
{
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH");
    string value1 = "#,##0.00";
    string value2 = ";";
    int result = string.Compare(value1, 0, value2, 0, 1);
    Console.WriteLine(result);
}

Notes:笔记:

  • As spender pointed out in this comment , the expected result of the comparison should be -1 .正如spender 在此评论中指出的那样,比较的预期结果应该是-1 not 1 .不是1

  • Assuming you've added the line changing the current culture for reproducibility reasons, your code can be condensed to:假设您出于可重现性原因添加了更改当前区域性的行,则您的代码可以压缩为:

     Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH"); var result = string.Compare("#", ";"); Console.WriteLine(result); // Writes 0 on .NET 5; writes -1 on .NET Framework
  • The exact value is irrelevant - only its sign ;确切的值是无关紧要的——只是它的符号 the documentation for the String.Compare method says: String.Compare方法的文档说:

    All overloads of the Compare method return a 32-bit signed integer indicating the lexical relationship between the two comparands. Compare方法的所有重载都返回一个 32 位带符号的 integer,指示两个比较数之间的词法关系。

    Value价值 Condition健康)状况
    Less than zero小于零 The first substring precedes the second substring in the sort order.在排序顺序中,第一个 substring 在第二个 substring 之前。
    Zero The substrings occur in the same position in the sort order, or length is zero.子字符串在排序顺序中出现在相同的 position 中,或者长度为零。
    Greater than zero大于零 The first substring follows the second substring in the sort order.在排序顺序中,第一个 substring 在第二个 substring 之后。

If you want an expected (ie negative) value, there are a few options available:如果你想要一个预期的(即负的)值,有几个选项可用:

  1. Use an overload which takes a StringComparison , eg Compare(String, String, StringComparison) :使用采用StringComparison的重载,例如Compare(String, String, StringComparison)

     Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH"); var result = string.Compare("#", ";", StringComparison.Ordinal); Console.WriteLine(result); // Writes "-24" on .NET 5 and .NET Framework
  2. Use a member of the String.CompareOrdinal overload group, eg CompareOrdinal(String, String) :使用String.CompareOrdinal重载组的成员,例如CompareOrdinal(String, String)

     Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH"); var result = string.CompareOrdinal("#", ";"); Console.WriteLine(result); // Writes "-24" on .NET 5 and .NET Framework

As for your question至于你的问题

Is anyone aware of the reason for this?有人知道这是为什么吗?

Hans Passant effectively provided it in his link to .NET globalization and ICU in this comment . Hans Passant在他的.NET globalization and ICU链接中有效地提供了这个评论

Quoting a relevant part of the document (emphasis mine):引用文档的相关部分(强调我的):

Prior to .NET 5, the .NET globalization APIs used different underlying libraries on different platforms. .NET 5之前,.NET全球化API在不同平台使用不同的底层库。 On Unix, the APIs used International Components for Unicode (ICU) , and on Windows, they used National Language Support (NLS) .在 Unix 上,API 使用Unicode (ICU) 的国际组件,在 Windows 上,它们使用国家语言支持 (NLS) This resulted in some behavioral differences in a handful of globalization APIs when running applications on different platforms.当在不同平台上运行应用程序时,这导致少数全球化 API 出现一些行为差异。 Behavior differences were evident in these areas:这些领域的行为差异很明显:

  • Cultures and culture data文化和文化数据
  • String casing套管
  • String sorting and searching字符串排序和搜索
  • Sort keys排序键
  • String normalization字符串规范化
  • Internationalized Domain Names (IDN) support国际化域名 (IDN) 支持
  • Time zone display name on Linux Linux 上的时区显示名称

Starting with .NET 5, developers have more control over which underlying library is used, enabling applications to avoid differences across platforms.从 .NET 5 开始,开发者可以更好地控制使用哪个底层库,使应用程序能够避免跨平台差异。

... ...

If you upgrade your app to target .NET 5, you might see changes in your app even if you don't realize you're using globalization facilities.如果您将应用程序升级到目标 .NET 5,即使您没有意识到您正在使用全球化设施,您也可能会看到应用程序发生变化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM