为什么string.StartsWith("\ u2D2D")总是返回true? [英] Why does string.StartsWith("\u2D2D") always return true?

查看:176
本文介绍了为什么string.StartsWith("\ u2D2D")总是返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was fiddling around with parsing in C# and found that for every string I tried, string.StartsWith("\u2D2D") will return true. Why is that?

似乎每个字符都可以使用.在.Net 4.5中尝试过此代码,调试器没有中断.

It seems it works with every char. Tried this code with .Net 4.5 the Debugger did not break.

for (char i = char.MinValue; i < char.MaxValue; i++)
{
    if(!i.ToString().StartsWith("\u2d2d"))
    {
        Debugger.Break();
    }
}

推荐答案

我想尝试一下.

根据我的了解,是U + 2D2D是在Unicode v6.1中添加的(/).

From what I get, is that U+2D2D was added in Unicode v6.1 (source / source).

.NET框架或本机调用更支持较低版本:

The .NET framework, or the native calls rather, support a lower version:

字符串中使用的区分文化的排序和大小写规则 比较取决于.NET Framework的版本.在.NET中 在Windows 8操作系统上运行的Framework 4.5,进行排序, 大小写,规范化和Unicode字符信息符合 Unicode 6.0标准.在其他操作系统上,它符合 Unicode 5.0标准. ()

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)

因此需要将其标记为可忽略的字符,其行为就像该字符不在那里一样.

Thus it is required to mark it as an ignorable character, which behaves just as if the character wasn't even there.

字符集包含可忽略的字符,这些字符是 在进行语言或文化敏感时不考虑 比较. ()

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).

string.StartsWith uses a similar implementation, but uses CompareInfo.IsPrefix(string, string, CompareOptions) instead.

这篇关于为什么string.StartsWith("\ u2D2D")总是返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆