2

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.

Neo
  • 1,031
  • 2
  • 11
  • 27
  • 3
    Did you import it correctly and added it to the classpath? You have to add the `org.apache.commons.lang.StringUtils` to your classpath manually or add it as a dependency to your gradle configuration or any other build tool you are using. – Markus Steppberger Mar 23 '19 at 09:34
  • show your imports. did you import specific Stringutils class – Onkar Musale Mar 23 '19 at 09:36
  • 2
    Which StringUtils class are you using? I'm not aware of one *in the JDK itself. There are no doubt *many* StringUtils classes in utility packages all over the place - which one are you using? (`isBlank` and `isEmpty` are part of `org.apache.commons.lang.StringUtils`) – Jon Skeet Mar 23 '19 at 09:36
  • I'm aware of only another used StringUtils, the one from Spring, which hasn't the methods you mentioned in older releases. – LppEdd Mar 23 '19 at 09:46

3 Answers3

2

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()VsString.isEmpty()

Vivek
  • 895
  • 11
  • 23
Umn23bra
  • 27
  • 5
1

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.

Markus Steppberger
  • 618
  • 1
  • 8
  • 18
  • It has the definition of a StringUtils class. Just not the right one. – LppEdd Mar 23 '19 at 10:05
  • 1
    As we do not have any code sample I only can give the general approach how to make it running. – Markus Steppberger Mar 23 '19 at 10:08
  • You do have the right answer. :D I was confused because there is a class `com.sun.tools.javac.util.StringUtils` in JDK. Didn't look at it carefully enough. – Neo Mar 25 '19 at 07:40
0

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Yeah I knew this. But still helpful. It's just that in most platforms or situations, they support up to Java 8. So I regretfully can't use this simple API. – Neo Mar 25 '19 at 07:37