HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

IsNullOrWhiteSpace check for Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
checkforisnullorwhitespacejava

Problem

I am needing to write my own IsNullOrWhiteSpace check for Java but am wondering if this is the best method to do so

public static boolean isStringNullOrWhiteSpace(String value) {
    if (isStringNullOrEmpty(value)){
        return true;
    }

    for (Character chars : value.toCharArray()){
        if (!Character.isWhitespace(chars)) return false;
    }

    return true;
}

Solution

I agree that the String should be left out of the name, as this is quite obvious in StringUtils. How about this short version?

public static boolean isNullOrWhiteSpace(String value) {
    return value == null || value.trim().isEmpty();
}

Code Snippets

public static boolean isNullOrWhiteSpace(String value) {
    return value == null || value.trim().isEmpty();
}

Context

StackExchange Code Review Q#69827, answer score: 17

Revisions (0)

No revisions yet.