Any scenario in which String.isEmpty() returns true and String.isBlank() returns false for the same input?

huangapple go评论61阅读模式
英文:

Any scenario in which String.isEmpty() returns true and String.isBlank() returns false for the same input?

问题

不是String.isBlank()等同于String.isEmpty() + *仅空白字符检查*吗?
在查看String.isBlank()的源代码时,当长度为零时它会返回true。但我想知道是否有任何情况下,对于相同的输入,isEmpty()会返回trueisBlank()会返回false

英文:

Isn't String.isBlank() equivalent to String.isEmpty() + *whitespace only check*?
While looking at the source code for String.isBlank(), it would return true when the length is zero. But I would like to know if there are any scenarios in which isEmpty() would return a true and isBlank() returns a false for the same input.

答案1

得分: 5

文档对于isEmpty()的说明:

当且仅当length()为0时,返回true。

关于isBlank()的文档:

如果字符串为空或仅包含空白代码点,则返回true;否则返回false。

强调是我添加的。

因此,isBlank() 似乎也涵盖了所有isEmpty()的情况。

只有一个情况下,isEmpty() 返回true,那就是空字符串 ""。如果提供空字符串作为参数,isBlank() 返回true

所以:不对。不存在这样的情况:isEmpty() 返回trueisBlank() 返回false

如果确实发生了这种情况,那就是一个软件 bug,前提是文档描述了预期的行为。否则,你可能需要问问薛定谔。

英文:

The docs say for isEmpty():

> Returns true if, and only if, length() is 0.

The docs of isBlank():

> Returns true if the string is empty or contains only white space codepoints, otherwise false.

<sup>Emphasis mine.</sup>

So isBlank() seems to cover also all cases of isEmpty().

There exists only one string where isEmpty() returns true, and that is an empty string &quot;&quot;. isBlank() returns true if an empty string is provided as parameter.

So: no. No case exists where isEmpty() returns true and isBlank() returns false.

And if it does, it is a software bug, provided that the documentation describes the intended behaviour. Or else you have to ask Schrödinger.

答案2

得分: 2

以下是翻译好的内容:

至少有一个输入,使得isEmpty()isBlank()产生相反的结果,那就是一个空格:

public class MyClass {
    public static void main(String args[]) {
      String s = " ";
      System.out.println("isBlank: "+s.isBlank());
      System.out.println("isEmpty: "+s.isEmpty());
    }
}

输出:

isBlank: true

isEmpty: false

相反的情况,即isBlank()返回false而isEmpty()返回true的情况是不会发生的,因为根据文档isBlank()仅在字符串为空或仅包含空白码点时返回true,而isEmpty()仅在length()为0时返回true。而长度为0的字符串没有被认为是空白的情况。

英文:

There is at least one input for which isEmpty() and isBlank() produce opposite results, and that's a space:

public class MyClass {
    public static void main(String args[]) {
      String s = &quot; &quot;;
      System.out.println(&quot;isBlank: &quot;+s.isBlank());
      System.out.println(&quot;isEmpty: &quot;+s.isEmpty());
    }
}

Output:

isBlank: true

isEmpty: false

The opposite case where isBlank()returns false and isEmpty returns true does not happen, because as per the documentation, isBlank() returns true only if the string is empty or contains only white space codepoints, and isEmpty() returns true if, and only if, length() is 0. And there is no case of a string of length 0 that is not considered blank.

huangapple
  • 本文由 发表于 2020年9月10日 22:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63832087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定