Joe Almore Joe Almore - 3 months ago 19
C# Question

String Comparison Length Mismatch

I am comparing two strings, one String I receive from a server with 32 characters with another one I calculate with the following code:

string getMd5(string fileName)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileName))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "‌​").ToLower();
}
}
}


The problem is, that even when the two strings seems identical, the comparison fails because the string returned by the function above contains more characters than the one I receive. Please, see picture attached:

enter image description here

So, how do I solve this?

Thank you.

Answer

That's because the "‌​" in your code actually contains an two invisible Unicode characters - a 'ZERO WIDTH NON-JOINER' (U+200C) and a 'ZERO WIDTH SPACE' (U+200B). My guess is that they got there because at some point the source code fragment went through a word processor such as Word or the like. Use string.Empty or have a free one - "".