Search code examples
androidwhitespace

android: check if the string not only white spaces


How can I check if a string contains anything other than whitespace?
This code didn't work:

String string = "   \n\n\t\t     ";
if(string.length()==0) doSomething();

since spaces and new lines have values.

Can anyone tell me how can I do it?

Note: minSDKVersion = 5

Regards :)


Solution

  • Try this:

    if (string.trim().length() == 0) { /* all white space */ }
    

    Alternatively, you can use a regular expression:

    if (string.matches("\\w*")) { . . . }