c# - 为什么 string.StartsWith ("\u2D2D") 总是返回 true?

标签 c# string unicode startswith

我在 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/

相关文章:

c# - 在 C# 中,为什么 string.Empty 是一个字段而不是常量?

python-3.x - 使用 Pandas 过滤具有多个值的单元格中的字符串

python: lower() 德语变音符号

c# - 测试 MVC Controller 方法上的属性数量

c# - UpdatePanel 中的 AddThis 控件 - 确保它们在异步回发后正确呈现

C# - 从客户端检查 TCP/IP 套接字状态

c# - Asp.net core 2.1 web api中的自定义授权

javascript - 为什么 JavaScript 对 plus 和 string 和 int 的计算方式不同?

c++ - 如何将 std::wstring 转换为 TCHAR*?

c# - 特殊字符未保存在 MS SQL 中