字符串比较长度不匹配

字符串比较长度不匹配

本文介绍了字符串比较长度不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在比较两个字符串,一个是从服务器接收的32个字符的字符串,另一个是我用以下代码计算的字符串:

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?

谢谢.

推荐答案

这是因为代码中的"‌​"实际上包含两个不可见的Unicode字符-一个零宽度非JOINER"(U + 200C)零宽度空间"(U + 200B).我的猜测是它们之所以到达那里,是因为源代码片段在某个时候经过了诸如Word之类的文字处理器.使用string.Empty或免费提供一个-"".

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 - "".

这篇关于字符串比较长度不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:31