字符串的长度不是我期望的

Ale*_*nov -4 c# string dictionary

下面的方法产生意外的输出:

public static void Main(string[] args)
    {
        var dict = new Dictionary<int, string>()
        {
            {       2, "0000 0000 0000 0000 0000 0000 0000 0010".Replace(" ", "") },
            {       0, "0000 0000 0000 0000 0000 0000 0000 0000".Replace(" ", "") },
            { 7529548, "0000 0000 ?0111 0010 1110 0100 0100 1100?".Replace(" ", "") }
        };

        foreach(var pair in dict)
        {
            Console.WriteLine(pair.Value.Length);
        }
        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

输出为: 32、32、34

如果我使用编译器设置,它也不会改变。

为什么会发生这种情况?为什么在最后一种情况下字符串长度可能不同?

Ry-*_*Ry- 6

在最后一个二进制字符串文字中:

  • 在之前0111,有一个U + 202D左至右覆盖
  • 之后1100,有一个U + 202C POP方向格式化

删除那些不可见的字符,您将获得所需的代码点长度。