user11626167
user11626167

Reputation:

How to return null when I have " " in java?

I am coming for a help where I have a json object and return a string and few of my strings are return as " " meaning there is a empty value but there is a space in the quotation. So, what I want to accomplish is I need help to check or implement a way to show null value instead of " ". is there a way with my code below? thanks for the help.

Upvotes: 0

Views: 97

Answers (1)

Zephyr
Zephyr

Reputation: 10253

A simple method that trims the String (removes leading/trailing whitespace) and then checks if the String is empty should do the trick:

public String getTrimmedString(String str) {
    if (str.trim().isEmpty()) {
        return null;
    } else {
        return str.trim();
    }
}

Upvotes: 3

Related Questions