温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java 中String.isBlank()的作用是什么

发布时间:2021-08-05 16:31:22 来源:亿速云 阅读:8152 作者:Leah 栏目:编程语言

这篇文章给大家介绍Java 中String.isBlank()的作用是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

学习使用 String.isBlank()方法确定给定的字符串是空白还是空或仅包含空格。 isBlank()方法已添加到 Java 11 中。

要检查给定的字符串甚至没有空格,请使用 String.isEmpty() 方法。

isBlank()方法

如果给定的字符串为空或仅包含空格代码点,则此方法返回 true ,否则返回 false 

它使用 Character.isWhitespace(char) 方法来确定空白字符。

/**
* returns 	- true if the string is empty or contains only white space codepoints
*		- otherwise false
*/

public boolean isBlank()

isBlank()示例

检查给定字符串是否为空的Java程序。

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println( "ABC".isBlank() );			//false

		System.out.println( " ABC ".isBlank() );		//false

		System.out.println( "  ".isBlank() );			//true

		System.out.println( "".isBlank() );				//true
	}
}

程序输出。

false
false
true
true

isBlank()与isEmpty()

两种方法都用于检查Java中的空白或空字符串。两种方法之间的区别在于,当且仅当字符串长度为0时, isEmpty()方法返回 true 

isBlank()方法仅检查非空白字符。它不检查字符串长度。

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println( "ABC".isBlank() );		//false
		System.out.println( "  ".isBlank() );		//true

		System.out.println( "ABC".isEmpty() );		//false
		System.out.println( "  ".isEmpty() );		//false
	}
}

程序输出。

false
true
false
false

关于Java 中String.isBlank()的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI