0 votes
1 view
in Java by (5.8k points)

I'm parsing HTML data. The String may be null or empty when the word to parse does not match.

So, I wrote it like this:

if(string.equals(null) || string.equals("")){

    Log.d("iftrue", "seem to be true");

}else{

    Log.d("iffalse", "seem to be false");

}

When I delete String.equals(""), it does not work correctly.

I thought String.equals("") wasn't correct.

How can I best check for an empty String?

1 Answer

0 votes
by (34.9k points)

right method to check for null or empty is like this:

if(str != null && !str.isEmpty()) { /* do your stuffs here */ }

...