Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the usage and difference of StringUtils.isBlank

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about the usage and difference of StringUtils.isBlank. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In our daily development, null detection should be the most commonly used operation. Therefore, there is always a reliance on the commons-lang3 package in the project. This package provides us with two methods of null detection, namely StringUtils.isEmpty (CharSequence cs) and StringUtils.isBlank (CharSequence cs). Let's take a look at the difference between the two methods.

1. StringUtils.isEmpty

The source code of isEmpty is as follows:

Public static boolean isEmpty (CharSequence cs) {return cs = = null | | cs.length () = = 0;}

This method determines whether the string is null or whether its length is zero.

"Test effect"

Public class BlankAndEmpty {

Public static void main (String [] args) {System.out.println (StringUtils.isEmpty (null)); / / true System.out.println (StringUtils.isEmpty (")); / / true System.out.println (StringUtils.isEmpty (")); / / false System.out.println (StringUtils.isEmpty ("\ t")); / / false System.out.println (StringUtils.isEmpty ("Java Journey")) / / false}} II. StringUtils.isBlank

The source code of isBlank is as follows:

Public static boolean isBlank (CharSequence cs) {int strLen = length (cs); if (strLen = = 0) {return true;} else {for (int I = 0; I < strLen; + + I) {if (! Character.isWhitespace (cs.charAt (I) {return false;}}

Return true;}}

The method of length (cs) is as follows

Public static int length (CharSequence cs) {return cs = = null? 0: cs.length ();}

This method not only determines whether the string is null and the length is zero, but also determines whether it is a space, and returns true if it is a space.

"Test effect"

Public class BlankAndEmpty {

Public static void main (String [] args) {

System.out.println (StringUtils.isBlank (null)); / / true System.out.println (StringUtils.isBlank (")); / / true System.out.println (StringUtils.isBlank (")); / / true System.out.println (StringUtils.isBlank ("\ t")); / / true System.out.println (StringUtils.isBlank ("Java Journey")) / / false}} 3. Summary isEmpty: return true if it is null or "". IsBlank: returns true if it is null or "" or spaces or tabs. "isBlank is more accurate in judging null". IV. Expansion

In actual development, in addition to several cases of isBlank null, in fact, we will also treat the "null" string as an empty string.

We need to determine that several fields cannot be empty at the same time, which is a bit cumbersome if we still use isBlank. We can use the variable parameters of String to provide the following utility classes.

Public class StringTool {

Public static boolean isNullStr (String... Args) {boolean falg = false; for (String arg: args) {if (StringUtils.isBlank (arg) | | arg.equals ("null")) {falg = true; return falg;}} return falg;}}

The advantages of this utility class are obvious: on the one hand, it determines the string "null", on the other hand, it has no limit on the number of parameters, and returns true as long as one parameter is empty.

After reading the above, have you mastered the usage and difference of StringUtils.isBlank? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report