StringUtils.isNotEmpty() Method

StringUtils.isNotEmpty() Method | In this blog, we will discuss the apache StringUtils isNotEmpty() method. This method is opposite to the StringUtils.isEmpty() method. In the isEmpty() method we find whether the given string is empty or not but in the isNotEmpty method we find the string is not empty or not. This method is present in the apache StringUtils class hence to use this method in our programs we need to import StringUtils class.

Apache StringUtils isNotEmpty() Method

The syntax for the method isNotEmpty() is as follows:-
public static boolean isNotEmpty(final CharSequence cs)
It checks if a CharSequence is not empty ("") and not null.

Parameters: CharSequence:- the CharSequence will be checked whether it is empty or not.
Return Type: boolean
Returns: true if the CharSequence is not empty and false if the string is empty.

The StringUtils.isNotEmpty() is simply implemented as follows:-

public static boolean isNotEmpty(final CharSequence cs) {
    return !isEmpty(cs);
}

Method Call Examples,

StringUtils.isNotEmpty(“Hello”)true
StringUtils.isNotEmpty(null)false
StringUtils.isNotEmpty("")  false
StringUtils.isNotEmpty(" ")  false

StringUtils.isNotEmpty() in Java Example-1

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "Hello, World!";
        System.out.println(StringUtils.isNotEmpty(string));

        string = null;
        System.out.println(StringUtils.isNotEmpty(string));
    }
}

Output:-

true
false

The string literal “Hello, World!” contains more than 0 characters and hence the StringUtils.isNotEmpty() method returns true, but later string variable contains reference to null therefore StringUtils.isNotEmpty() method returns false.

StringUtils.isNotEmpty() Java Example-2

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "null";
        System.out.println(StringUtils.isNotEmpty(string));

        string = "   \n\t";
        System.out.println(StringUtils.isNotEmpty(string));
    }
}

Output:-

true
true

In the string literal “null” there are 4 valid characters ‘n’, ‘u’, ‘l’, ‘l’, and have string length greater than 0 therefore StringUtils.isNotEmpty() method returns true. In the next line string variable contains only whitespaces. If it is StringUtils.isNotBlank() method then it will return false because they exclude the whitespaces but when it comes to StringUtils.isNotEmpty() method then it includes the whitespace therefore it returns true.

StringUtils.isNotEmpty() In Java Example-3

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "";
        System.out.println(StringUtils.isNotEmpty(string));
    }
}

Output:-

false

The string variable contains an empty string. The length of an empty string is 0 therefore the StringUtils.isNotEmpty() method returns false.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.