String Comparison Length Mismatch

自古美人都是妖i 提交于 2020-01-05 05:32:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!