Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k 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 (46k points)

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

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

Related questions

0 votes
1 answer
0 votes
2 answers
asked Jul 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...