Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check empty spaces in java

Tags:

java

String selectedVal = "";

for (SelectItem item : filterItems) {
                selectedVal = item.getValue().toString();
                break;
            }

I am getting selectedVal=" " how to check this empty space in java.

I tried with if(!selectedVal.equals("") and if(!selectedVal.isEmpty()) but condition getting true.How to check more than one empty space ?

like image 869
user441978 Avatar asked Nov 29 '22 05:11

user441978


1 Answers

You can trim() your string before checking with isEmpty()

boolean isEmpty = myString.trim().isEmpty();

Beware of isEmpty(), it's only available since Java SE 6


Resources :

  • javadoc - String.trim()
  • javadoc - String.isEmpty()
like image 83
Colin Hebert Avatar answered Dec 09 '22 20:12

Colin Hebert