我在 C# 中摆弄解析,发现对于我尝试的每个字符串,string.StartsWith("\u2D2D")
都会返回 true。这是为什么?
它似乎适用于每个字符。使用 .Net 4.5 尝试此代码,调试器没有中断。
for (char i = char.MinValue; i < char.MaxValue; i++)
{
if(!i.ToString().StartsWith("\u2d2d"))
{
Debugger.Break();
}
}
最佳答案
我想我会试一试。
据我了解,U+2D2D 是在 Unicode v6.1 ( source/source ) 中添加的。
.NET 框架,或者说 native 调用,支持较低版本:
The culture-sensitive sorting and casing rules used in string comparison depend on the version of the .NET Framework. In the .NET Framework 4.5 running on the Windows 8 operating system, sorting, casing, normalization, and Unicode character information conforms to the Unicode 6.0 standard. On other operating systems, it conforms to the Unicode 5.0 standard. (source)
因此需要将其标记为可忽略字符,其行为就好像该字符根本不存在一样。
Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. (source)
例子:
var culture = new CultureInfo("en-US");
int result = culture.CompareInfo.Compare("", "\u2D2D", CompareOptions.None);
Assert.AreEqual(0, result);
string.StartsWith
使用类似的实现,但使用 CompareInfo.IsPrefix(string, string, CompareOptions)
代替。
关于c# - 为什么 string.StartsWith ("\u2D2D") 总是返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663828/