I have a list of String
s, and I want to concatenate them with spaces in between. So I\'m using StringBuilder
. Now if any of the String
s ar
I'm not sure why you'd expect it to come out empty, given that the documentation is pretty clear:
If str is null, then the four characters "null" are appended.
Basically you need to either not call append at all if you have a null reference, or switch the value for "".
You could write a method to do this substitution if you find yourself doing it a lot:
public static String nullToEmpty(String text) {
return text == null ? "" : text;
}
Indeed, I've just looked at the Guava documentation and the Strings class has exactly that method (but with a parameter called string
instead of text
).