4
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 ?

8 Answers 8

9

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 :

Sign up to request clarification or add additional context in comments.

1 Comment

If isEmpty() is not available, you can also check the String length: if (s.length() == 0) {}
8

I use this all the time:

public static boolean isBlank(String s)
{
    return (s == null) || (s.trim().length() == 0);
}

Returns true on null, empty string, or whitespace only.

Comments

2

For such a simple test, using an external library is not a good idea, but if you need String manipulation (left and right padding, etc.), you can go for Apache commons lang and the StringUtils.isEmpty() method.

More recently, you can use the Google Guava library and the class Strings. This library has a lot of usefull util methods (null handling, etc.). Once again, use this library only if you have others needs than checking empty strings.

Comments

2

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

Comments

1

I use a routine similar to what Grodriguez posted. It ends up in a util/BagOTricks.java file in every project. My routine does a similar check and returns a null as a space, or the trimmed input string.

Comments

0

I am not a java programmer, but with regex, \s means white space.

This link might also be useful:

 /**
   * Helper function for making null strings safe for comparisons, etc.
   *
   * @return (s == null) ? "" : s;
   */
  public static String makeSafe(String s) {
    return (s == null) ? "" : s;
  }

/**
   * Helper function for null, empty, and whitespace string testing.
   *
   * @return true if s == null or s.equals("") or s contains only whitespace
   *         characters.
   */
  public static boolean isEmptyOrWhitespace(String s) {
    s = makeSafe(s);
    for (int i = 0, n = s.length(); i < n; i++) {
      if (!Character.isWhitespace(s.charAt(i))) {
        return false;
      }
    }
    return true;
  }

5 Comments

Why regexes when you don't have a regular expression ?
@Colin Hebert: He wants to test for a condition of "only space(s)." That's a classic 1:1 correspondence to a regexp, in my view. Especially if it turns out his data contains whitespace characters other than space. Shimmy's procedural solution probably works well enough and certainly has better performance, but it's far from an elegant translation of the problem.
can you expand on what the makeSafe method does?
I am not in the java business anyway, just googled around and that's what I've found, I am a .NET user where the IsNullOrWhiteSpace method is out-the-box (in .NET 4), otherwise I would've checked isnull->trim->length. Also in .NET there are extension methods which make the solution much more convenient.
@fahd, as I said, I am not a java programmer at all, and as stated in my post, the code is taken from the link above it: java2s.com/Code/Java/Data-Type/Blankstringemptyorwhitespace.htm
0

You can use this method:

public boolean isNullOrEmpty(String s) {
    return s == null || s.trim().isEmpty();
}

Method returns true when passed string is null or empty / only contains blank spaces.

Comments

0

Using the Apache Commons Lang library, you can use StringUtils.isEmpty() to check if a String is empty ("") or null, and StringUtils.isBlank() to check if a String is whitespace, empty ("") or null.

Note the differences:

isEmpty()

StringUtils.isEmpty(null)      = true
StringUtils.isEmpty("")        = true
StringUtils.isEmpty(" ")       = false
StringUtils.isEmpty("bob")     = false
StringUtils.isEmpty("  bob  ") = false

isBlank()

StringUtils.isBlank(null)      = true
StringUtils.isBlank("")        = true
StringUtils.isBlank(" ")       = true
StringUtils.isBlank("bob")     = false
StringUtils.isBlank("  bob  ") = false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.