简体   繁体   English

string.IsNullOrEmpty & string.IsNullOrWhiteSpace 为空字符串返回 false

[英]string.IsNullOrEmpty & string.IsNullOrWhiteSpace return false for empty string

I have run into a curious case where a block of code that is designed to weed out blank strings, periods dashes and the like after a paragraph of text is processed from the MSFT Azure Phrase Breaker.我遇到了一个奇怪的案例,其中一段代码旨在从 MSFT Azure Phrase Breaker 处理一段文本后清除空白字符串、句号破折号等。 I could use help figuring out how to debug the issue.我可以使用帮助找出如何调试问题。

The following block of code returns true when given a value of "" .当给定值""时,以下代码块返回true Obviously the expectation is the method should return false after the first if statement.显然,期望是该方法应该在第一个if语句之后返回false Out of 899 phrases to be looked at, only two seem to have this problem.在要查看的 899 个短语中,似乎只有两个有这个问题。 Happens on another machine as well.也发生在另一台机器上。

public static bool IsSentenceTranslatable(string sentence)
{
    string trimmedSentence = sentence.Trim();

    if (string.IsNullOrEmpty(trimmedSentence) || string.IsNullOrWhiteSpace(trimmedSentence))
    {
        return false;
    }

    switch (trimmedSentence)
    {
        case " ":
        case ".":
        case "-":
        case "·":
            return false;
    }

    return true;
}

Here is a snapshot of the debugger.这是调试器的快照。

调试器快照

Could it be a bug in Visual Studio or .NET?它可能是 Visual Studio 或 .NET 中的错误吗? I tried using the various visualizers, but still couldn't see anything in the box.我尝试使用各种可视化工具,但仍然看不到盒子里的任何东西。 .NET 4.5 C# 7.3 .NET 4.5 C# 7.3

Try to get the string's byte representation.尝试获取字符串的字节表示。 I suspect that it actually contains some character which is invisible in VS debugger but doesn't count as a whitespace.我怀疑它实际上包含一些在 VS 调试器中不可见但不算作空格的字符。

See this questions for hints:请参阅此问题以获取提示:

UPD: since your Watch window shows that after the call string trimmedSentence = sentence.Trim() you have trimmedSentence.Length == 1 , I'd upgrade my suspicion to certainty. UPD:由于您的 Watch 窗口显示在调用string trimmedSentence = sentence.Trim()您已经trimmedSentence.Length == 1 ,我将我的怀疑升级为确定性。

As stated in my comment, in that screenshot you can see that trimmedSentence.Length is 1, therefore it's not empty, and its contents is definitely not a standard space.正如我的评论中所述,在该屏幕截图中您可以看到trimmedSentence.Length为 1,因此它不是空的,其内容绝对不是标准空间。 If the string appears empty, it's because it has one of those so-called invisible characters.如果字符串显示为空,那是因为它具有那些所谓的不可见字符之一。 To check what your string has, you can directly access that character by doing trimmedSentence[0] .要检查您的字符串有什么,您可以通过执行trimmedSentence[0]直接访问该字符。

If that character will appear often, you might want to consider doing this:如果该角色经常出现,您可能需要考虑这样做:

string trimmedSentence = sentence.Trim().Replace("<this special character>", "");

Alternatively, you can create that replaceable string from the Unicode value by doing Char.ConvertFromUtf32(yourCharCode).ToString() .或者,您可以通过执行Char.ConvertFromUtf32(yourCharCode).ToString()从 Unicode 值创建该可替换字符串。 You cannot use the Replace overload that uses character parameters, as there is no "empty" character, only an empty string.您不能使用使用字符参数的Replace重载,因为没有“空”字符,只有空字符串。 You should be able to get this value while debugging.您应该能够在调试时获得此值。 Or if necessary, by doing (int)trimmedSentence[0] .或者,如有必要,通过执行(int)trimmedSentence[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM