简体   繁体   中英

why my StringUtils class doesn't have isBlank()

I can't find StringUtils.isBlank() in my JDK.

People says on google that StringUtils.isBlank() can be used to detect a blank string. But my IntelliJ tells me that this function doesn't exist. Nevertheless, StringUtils.isEmpty doesn't exist too. But I saw many people are using it. What am I possibly doing wrong?

I'm using IntelliJ released on Jan 9 2019, Windows, JDK 12. Also tried on JDK 9.

You can use new String class function added since JDK11 , the String.isBlank()

Docs here

isBlank
public boolean isBlank()

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

Also, as per your use-case advise to go through : StringUtils.isBlank() Vs String.isEmpty()

The library you want to use is the org.apache.commons.lang.StringUtils and you have to add it to your classpath manually or add it as a dependency to your gradle configuration or any other build tool you are using.

IntelliJ does not know the library out of the box as it is not part of the JDK.

StringUtils is from a third party library called 'apache commons'; it is not part of the library that java includes out of the box, you'd have to add it manually.

But there is no need for that. Starting with JDK11, strings themselves have an isBlank method:

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

would print true . Perhaps you read something about isBlank and got this new (introduced in JDK11) API confused with the utility method from the apache commons library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM