Reputation: 11592
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.
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...
My code is :
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...
}
}
}
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.
What is the solution for removing the empty char or any other spurious characters at the end of the currInnerText string?
Upvotes: 27
Views: 32617
Reputation: 14515
In addition to using characters that look like other characters, but are actually different, this can occur when using Reflection. Reflection boxes the values twice into new objects and ==
will compare by reference. Try using object.Equals(currentValue, newValue)
instead.
Upvotes: 1
Reputation: 9658
In my case, the difference was different encoding of space character, one string contained non-breaking space (160) and the other one contained normal space (32)
it can be solved by
string text1 = "String with non breaking spaces.";
text1 = Regex.Replace(text1, @"\u00A0", " ");
// now you can compare them
Upvotes: 14
Reputation: 23786
Before you call .Equals, try this:
if (currInnerText.Length != paraText.Length)
throw new Exception("Well here's the problem");
for (int i = 0; i < currInnerText.Length; i++) {
if (currInnerText[i] != paraText[i]) {
throw new Exception("Difference at character: " + i+1);
}
}
That should throw an exception if Equals returns false and should give you an idea what's going.
Upvotes: 8
Reputation: 9519
Try this
String.Equals(currInnerText, paraText, StringComparison.InvariantCultureIgnoreCase);
Upvotes: 30
Reputation: 446
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()
Upvotes: 13