问题
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:
So, how do I solve this?
Thank you.
回答1:
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 - ""
.
来源:https://stackoverflow.com/questions/39151865/string-comparison-length-mismatch