为什么 string.StartsWith("\u2D2D") 总是 return 为真?

Why does string.StartsWith("\u2D2D") always return true?

我在 C# 中摆弄解析,发现对于我尝试的每个字符串,string.StartsWith("\u2D2D") 都将 return 为真。这是为什么?

它似乎适用于每个字符。使用 .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 框架,或者更确切地说是原生调用,支持较低版本:

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) 代替。