C#String.Equals对相同的字符串返回false [英] C# String.Equals returns false on identical strings

查看:641
本文介绍了C#String.Equals对相同的字符串返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目上,对于其中的一部分,我需要比较2个字符串. 我的问题是,每当我尝试比较它们时,我总是会得到错误的(==, .Equals(), String.Equals()-即使我有2个完全相同的字符串,它们都将返回false.

I am working on a project and for a part of it I need to compare 2 strings. My issue comes that whenever I try to compare them I always get false (==, .Equals(), String.Equals() - they all return false, even though I have 2 completely identical strings)

这是我的代码的一部分.

Here is a part of my code.

var tagType = JObject.Parse(json).First.First.ToString();
foreach (var type in assembly.ExportedTypes)
{
    var name = tagType;
    var currentType = type.Name;

    var a = name.Length;
    var b = currentType.Length;

    var result = currentType == name;
    var result1 = currentType.Equals(name);
    var result2 = String.Equals(name, currentType, StringComparison.CurrentCulture);
    var result3 = String.Equals(name, currentType, StringComparison.InvariantCulture);
    var result4 = String.Equals(name, currentType, StringComparison.Ordinal);
    var result5 = String.Equals(name, currentType, StringComparison.CurrentCultureIgnoreCase);
}

现在,当调试我的foreach时,我最终到达name和currentType都等于相同字符串-"AutoIncrementTag"的地步.在同一点,它们的长度(a和b)相等-16个字符.

Now when debugging my foreach, I eventually reach a point where name and currentType both equal the same string - "AutoIncrementTag". At that same point their lengths (a and b) are equal - 16 characters.

这是调试输出的样子:

//name -        "AutoIncrementТаg"
//currentType - "AutoIncrementTag"

//a - 16
//b - 16

// result - false
// result1 - false
// result2 - false
// result3 - false
// result4 - false
// result5 - false

以下所有比较均返回false.

And ALL of the comparisons below return false.

我什至尝试用"name"和currenType来创建一个新的字符串.没什么.

I even tried creating a new string out of both "name" and currenType. And nothing.

我真的被困在这里.两个相同的字符串(长度相同,因此没有隐藏的字符)如何通过任何形式的比较都返回false.

I am really stuck here. How can two identical strings (same length, so no hidden characters) return false with any kind of comparison.

推荐答案

倒数第二个和倒数第二个字符不同.

The second last and third last characters are not the same.

倒数第二个字符之一是 http://www.fileformat .info/info/unicode/char/0061/index.htm ,另一个是 http://www.fileformat.info/info/unicode/char/0430/index.htm .它们外观相同,但实际上并不相同.

One of the second last characters is http://www.fileformat.info/info/unicode/char/0061/index.htm and the other is http://www.fileformat.info/info/unicode/char/0430/index.htm . They look the same, but aren't actually the same.

要查看它,请运行以下程序:

To see it, run this program:

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static string GetEscapeSequence(char c)
        {
            return "\\u" + ((int)c).ToString("X4");
        }

        static void Main(string[] args)
        {
            var name = "AutoIncrementТаg";
            var currentType = "AutoIncrementTag";

            foreach (var character in name)
            {
                Console.WriteLine(GetEscapeSequence(character));
            }

            Console.WriteLine("second string");
            foreach (var character in currentType)
            {
                Console.WriteLine(GetEscapeSequence(character));
            }

            Console.ReadLine();
        }
    }
}

这篇关于C#String.Equals对相同的字符串返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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