Java 11中的isEmpty()和isBlank()方法之间的区别 [英] Difference between isEmpty() and isBlank() Method in java 11

查看:275
本文介绍了Java 11中的isEmpty()和isBlank()方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 11 已向java.lang.String类添加了新的实例方法isBlank().

Java 11 has added A new instance method isBlank() to java.lang.String class.

现有isEmpty和新添加的isBlank()方法之间的基本区别是什么?

What's the basic difference between the existing isEmpty and newly added isBlank() method?

推荐答案

isEmpty()

java字符串isEmpty()方法检查此字符串是否为空.如果字符串的长度为0,则返回 true ,否则返回false.

The java string isEmpty() method checks if this string is empty. It returns true, if the length of the string is 0 otherwise false e.g.

System.out.println("".isEmpty()); // Prints - True
System.out.println(" ".isEmpty()); //Prints - False 

Java 11-isBlank()

Java 11 - isBlank()

如果字符串为空或仅包含空格,则新的实例方法java.lang.String.isBlank() 返回true , 空格定义为传递给Character#isWhitespace(int)时返回true的任何代码点.

The new instance method java.lang.String.isBlank() returns true if the string is empty or contains only white space, where whitespace is defined as any codepoint that returns true when passed to Character#isWhitespace(int).

boolean blank = string.isBlank();


在Java 11之前

boolean blank = string.trim().isEmpty();

Java 11之后

boolean blank = string.isBlank();

这篇关于Java 11中的isEmpty()和isBlank()方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆