Take the 2-minute tour ×

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().

share|improve this question
What about iterating on the characters until end of string or character not blank? –  Laurent Pireyn Apr 28 '11 at 13:59
I always do exactly trim().length(), and do not lose my sleep over such minor inefficiency. –  Vladimir Dyuzhev Apr 28 '11 at 14:03
Does you magic tool tell, why it is better? Maybe it's better to ignore that suggestion ;) –  Andreas_D Apr 28 '11 at 14:03
because trim() creates a new instance of String(), I guess. –  Vladimir Dyuzhev Apr 28 '11 at 14:07
@road: trim() returns a new String only if its content is different from the String it's been called on, otherwise it returns itself (that's even documented so you can rely on it!). So the new String instance is only created in the (usually rare) case where there's a space at the start or end of the String. –  Joachim Sauer Apr 28 '11 at 14:21
show 13 more comments

8 Answers

up vote 2 down vote accepted

I'd use the Guava CharMatcher class:

boolean onlyWhitespace = CharMatcher.WHITESPACE.matchesAllOf(input);
share|improve this answer
Perhaps s.matches("\\s*"); would do the same then? I personally prefer to stick to JRE libs whenever possible... –  Vladimir Dyuzhev Apr 28 '11 at 14:05
@road: in practice: yes, it will be the same. Strictly speaking it only tests a limited subset of whitespace characters, while CharMatcher.WHITESPACE contains all Unicode whitespace characters. Also, .matches() would require compiling the regex each time which might be slower. –  Joachim Sauer Apr 28 '11 at 14:07
Not sure "\\s" matches a limited subset. And regexp -- I can compile it once and use in an utility method StringUtils.isBlank(s). –  Vladimir Dyuzhev Apr 28 '11 at 14:09
@road: according to Pattern it matches [ \t\n\x0B\f\r]. This means that many other Whitespace characters such as U+200A, U+202F, U+3000, ... are not detected). I don't know how much difference that'll make in practice, however. –  Joachim Sauer Apr 28 '11 at 14:11
Yes, just read docs there and there. Need to file a bug for Su... Oracle, I mean! :) –  Vladimir Dyuzhev Apr 28 '11 at 14:13
add comment

I always like to use the Apache Commons StringUtils library. It has isEmpty() and is isBlank() which handles whitespace.


Not to mention the numerous other helpful methods in that class and the library in general.

share|improve this answer
Yes, Apache StringUtils is just great! –  Jan Zyka Apr 28 '11 at 14:20
(learned from @Joachim Sauer) Doesn't support all Unicode as CharMatcher does! ;) –  Vladimir Dyuzhev Apr 28 '11 at 14:20
If you're refering to his comments below 2 of the 3 unicode characters he lists work with StringUtils. Unless I'm doing something wrong, StringUtils.isBlank((Character.toString('\u200A')); returns true and isEmpty returns false which should be correct. Same for \u3000. \u202F isn't considered blank by Java Character.isWhiteSpace so I'm not sure I'd want StringUtils to return true for that. –  Jberg Apr 28 '11 at 15:59
StringUtils.isEmpty() doesn't trim the string since v2. From javadoc: "NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank()." commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/… –  indusBull Oct 4 '13 at 15:42
add comment

Okay guys, I have found it finally from PMD rules of InefficientEmptyStringCheck:

Since: PMD 3.6
String.trim().length() is an inefficient way to check if a String is really empty, as it creates a new String object just to check its size. Consider creating a static function that loops through a string, checking Character.isWhitespace() on each character and returning false if a non-whitespace character is found.

This is only a suggestion from PMD. To adopt it or not is depending on which has prioriy: the efficiency of programs or the time of programmers.

share|improve this answer
add comment

Java 6 has introduced String.isEmpty(), so you could use it in conjunction with String.trim(). You can also use regular expressions, for example using such a condition: !str.matches("\\s*").

share|improve this answer
the issue is trim(). it creates (or may create an new instance of String) which is not efficient. –  Vladimir Dyuzhev Apr 28 '11 at 14:06
add comment

There is a method in String for this exact purpose.

String emptyString = "";

This will return true.

share|improve this answer
But this is not the same as String.trim().length() == 0! Just change your first line to String emptyString = "\n". Your code will return false, whereas String.trim().length() == 0 will return true. –  THelper Nov 29 '13 at 13:11
add comment

If you want to test for a string that has a zero length than using isEmpty() or length() == 0 is the best way.

If you want to test if the string only contains whitespaces, then searching for the first non-whitespace character is more efficient because not intermediate object is created (as with trim())

But in any case I too recommend Apache's commons StringUtils.isEmpty()as it nicely encapsulates all this.

share|improve this answer
add comment

If you want to test, whether it only contains whitespace characters, you can use RegEx


Thinks it's more efficient than trim().isEmpty(), especially if you expect whitespaces and have long Strings, though I'm not sure how much effort it takes to compile the RegEx.

share|improve this answer
add comment

(Best way) string.equals("")

But also,


share|improve this answer
string.length() == 0 is far more efficient than string.equals("") –  a_horse_with_no_name Apr 28 '11 at 14:04
Not really a solution for strings with whitespaces only. –  DixonD Apr 28 '11 at 14:16
string.equals(null) always returns false or throws NullPointerException. –  Natix Feb 5 at 14:24
add comment

Your Answer


