需要了解为什么 string.StartsWith() 在应该为假时为真
Need understanding as to why string.StartsWith() is true when it should be false
所以我通过 ftp 下载了这个文件。该文件只是我工作的公司系统的配置文件。
下载文件后,我打开文件并处理文件。
文件处理的一部分是检查一行是否以 Unicode 字符 \u001a
.
开头
这是让我感到困惑的地方,因为 .StartsWith("\u001a")
总是 true
,但我不明白为什么。如果我在 Notepad++ 或十六进制编辑器中查看文件,我就是看不到。
所以我在这里遗漏了一些东西。
这是一个最简单的例子 (fiddle):
// prints True in .NET 5
Console.WriteLine("Hello".StartsWith("\u001a"));
因为一个breaking change in the globalizations APIs in .NET 5。您可以选择以下方法之一
- 使用
StringComparison.Ordinal
或OrdinalIgnoreCase
- 使用 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
设置为值 true
或 1
.
所以我通过 ftp 下载了这个文件。该文件只是我工作的公司系统的配置文件。
下载文件后,我打开文件并处理文件。
文件处理的一部分是检查一行是否以 Unicode 字符 \u001a
.
这是让我感到困惑的地方,因为 .StartsWith("\u001a")
总是 true
,但我不明白为什么。如果我在 Notepad++ 或十六进制编辑器中查看文件,我就是看不到。
所以我在这里遗漏了一些东西。
这是一个最简单的例子 (fiddle):
// prints True in .NET 5
Console.WriteLine("Hello".StartsWith("\u001a"));
因为一个breaking change in the globalizations APIs in .NET 5。您可以选择以下方法之一
- 使用
StringComparison.Ordinal
或OrdinalIgnoreCase
- 使用 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
设置为值true
或1
.