javastring

What's the better way to check if a String is empty than using String.trim().length() in Java 5/6?


There probably is a method to return the index of the first non-blank char in a String in Java5/6. But I cannot find it any more. A code anylizing tool says it is better than checking String.trim().length().


Solution

  • I'd use the Guava CharMatcher class:

    boolean onlyWhitespace = CharMatcher.WHITESPACE.matchesAllOf(input);