【问题标题】:Need understanding as to why string.StartsWith() is true when it should be false需要了解为什么 string.StartsWith() 应该为假时为真
【发布时间】:2021-09-24 11:14:52
【问题描述】:

所以我有这个文件,我通过 ftp 下载。该文件只是我工作的公司系统的配置文件。 下载文件后,我打开文件并处理文件。 文件处理的一部分是检查一行是否以Unicode字符\u001a开头。

这就是我难住的地方,因为.StartsWith("\u001a") 总是true,但我不明白为什么。如果我在 Notepad++ 或十六进制编辑器中查看该文件,我只是看不到。

所以我在这里缺少一些东西。

这是一个最小的例子 (fiddle):

// prints True in .NET 5
Console.WriteLine("Hello".StartsWith("\u001a"));

【问题讨论】:

标签: c# string .net-5


【解决方案1】:

这是因为breaking change in the globalizations APIs in .NET 5。您可以选择以下方法之一

  1. 使用StringComparison.OrdinalOrdinalIgnoreCase
  2. 使用 NLS 代替 ICU,with one of the following ways
  • 在项目文件中:
<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>
  • 在 runtimeconfig.json 文件中:
{
  "runtimeOptions": {
     "configProperties": {
       "System.Globalization.UseNls": true
      }
  }
}
  • 通过将环境变量DOTNET_SYSTEM_GLOBALIZATION_USENLS 设置为值true1

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2018-07-01
    • 2020-08-23
    • 2023-04-02
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多