代码之家  ›  专栏  ›  技术社区  ›  Gautham M

任何情况下String.i空()返回true和String.is空白()对于相同的输入返回false?

  •  0
  • Gautham M  · 技术社区  · 4 年前

    不是吗 String.isBlank() 相当于 String.isEmpty() + *whitespace only check* ? 同时查看源代码 String.is空白() ,当长度为零时将返回true。但我想知道是否有任何情况 isEmpty() 将返回一个 true isBlank() false 对于相同的输入。

    0 回复  |  直到 4 年前
        1
  •  5
  •   MC Emperor    4 年前

    isEmpty()

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

    的文件 isBlank() :

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

    所以呢 isBlank() 似乎也涵盖了所有 isEmpty() .

    只有一个字符串 isEmpty() true ,这是一个空字符串 "" . isBlank() 如果提供空字符串作为参数。

    所以: . 不存在这样的情况 isEmpty() 返回true和

    如果是这样,那就是一个软件错误,前提是文档描述了预期的行为。否则你得问问施廷格。

        2
  •  1
  •   JustAnotherDeveloper    4 年前

    至少有一个输入 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:真

    isBlank() isEmpty 返回true不会发生,因为根据 documentation , 仅当字符串为空或仅包含空白代码点时返回true,并且 isEmpty() 当且仅当, length() 是0。长度为0的字符串不被视为空白。