字符串等于()方法失败,即使两个字符串在C#中一样吗? [英] String Equals() method fails even though the two strings are same in C#?

查看:166
本文介绍了字符串等于()方法失败,即使两个字符串在C#中一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较两个字符串是否相等或不使用equals String类的()方法C#。不过即使这两个字符串是一样的,我的条件检查失败

I want to compare whether two strings are equal or not in C# using the Equals() method of the string class. But even though both strings are same, my conditional check is failing.

我看到这两个字符串相等,也验证了该在的 http://text-compare.com/ 网站。我不知道什么是这里的问题...

I have seen that both strings are equal and also verified this at the http://text-compare.com/ site. I don't know what is the issue here...

我的代码是:

protected string getInnerParaOnly(DocumentFormat.OpenXml.Wordprocessing.Paragraph currPara, string paraText)
        {
            string currInnerText = "";
            bool isChildRun = false;

        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(currPara.OuterXml);
        XmlNode newNode = xDoc.DocumentElement;

        string temp = currPara.OuterXml.ToString().Trim();

        XmlNodeList pNode = xDoc.GetElementsByTagName("w:p");
        for (int i = 0; i < pNode.Count; i++)
        {
            if (i == 0)
            {
                XmlNodeList childList = pNode[i].ChildNodes;
                foreach (XmlNode xNode in childList)
                {
                    if (xNode.Name == "w:r")
                    {
                        XmlNodeList childList1 = xNode.ChildNodes;
                        foreach (XmlNode xNode1 in childList1)
                        {
                            if (xNode1.Name == "w:t" && xNode1.Name != "w:pict")
                            {
                                currInnerText = currInnerText + xNode1.InnerText;
                            }
                        }
                    }
                }
              if (currInnerText.Equals(paraText))
              {
                  //do lot of work here...
              }
   }
}

当我把在突破点,走一步看一步经过,看着每一个字符,那么在最后currInnerText指数的差异。它看起来像一个空的字符。不过,我已经使用了修剪()函数。这是在调试过程中拍摄的照片。

When I put a break point in and go through step by step, watching each and every character, then there is a difference in currInnerText last index. It looks like an empty char. But I already used the Trim() function. This is the picture captured during the debug process.

什么是去除空char或在currInnerText字符串的结尾其他任何虚假字符解决?

What is the solution for removing the empty char or any other spurious characters at the end of the currInnerText string?

推荐答案

尝试把一个断点,并检查长度。此外,在某些情况下,如果区域是不一样的,在平等功能不会导致真实的。你可以尝试另一种方法(检查长)是印刷都这样---字符串1 --- --- ---字符串2,这样一来,你可以看到,如果您有任何尾随空格。为了解决这个问题,你可以使用string1.trim()

Try putting a breakpoint and checking the length. Also, in some cases, if the locale is not the same, the equals function does not result in true. Another method you could try(checking the length) is printing both like this ---string1---, ---string2---, this way, you could see if you have any trailing spaces. To fix this you can use string1.trim()

这篇关于字符串等于()方法失败,即使两个字符串在C#中一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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