1

I'm trying to adapt a code which uses String.isBlank(text). This method was introduced in Java 11, but my current project uses Java 8 and I don't want to upgrade. Is there anywhere the source code of the isBlank method or how can I replace it?

I tried make a method but I'm not sure what check should I put in there:

/**
 * @param text - the text to check
 * @return {@code true} if {@code text} is empty or contains only white space codepoints
 */
public static final boolean isBlank(final String text)
{
    if (text == null || text.isEmpty())
    {
        return true;
    }
    
    for (final char c : text.toCharArray())
    {
        //Check here
    }
    
    return false;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I want match my method exactly with the isBlank of java 11 – KbantikiMixaniki Aug 12 '20 at 11:56
  • Does this answer your question? [Difference between isEmpty() and isBlank() Method in java 11](https://stackoverflow.com/questions/51299126/difference-between-isempty-and-isblank-method-in-java-11) – Sudoss Aug 12 '20 at 11:57
  • [You cannot add methods to the java string library as the class is final](https://stackoverflow.com/questions/81786/can-i-add-new-methods-to-the-string-class-in-java). Instead you should make your own method to use. – Conor Egan Aug 12 '20 at 11:57

5 Answers5

5

In the class org.apache.commons.lang3.StringUtils, the function isBlank is like this:

public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}

It works well for Java 8, you just have to import it

TheTisiboth
  • 1,431
  • 1
  • 6
  • 13
3

Before Java 11: string.trim().isEmpty();

so, if you want wrap isBlank function, you can do as follow:

public static final boolean isBlank(final String text) {
    return text == null || text.trim().isEmpty();
} 
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
1

I had the same issue that you, a Java 11 project which I had to downgrade to Java 8. You can import Apache Commons in your project:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

Then only what you need to do is call StringUtils.isBlank(string) it will return true or false

Murilo Góes de Almeida
  • 1,588
  • 5
  • 20
  • 42
0

You can use Guava library, and

Strings.nullToEmpty(str).trim().isEmpty()

to replicate the isBlank behaviour.

merc-angel
  • 394
  • 1
  • 5
  • 13
0

In addition to other answers (importing libraries), you can define your own function in a StringUtils class, then you can add any other String utils operations.

The current method you want would look like this:


public static boolean isNullOrEmpty(String text) {
        boolean isNull = true;
        if (text != null) {
            if (!text.trim().equals("")) {
                isNull = false;
            }
        }
        return isNull;
    }

Also, through this you don't have to import any library