tags:

views:

124

answers:

3

Very tired this morning so if this is obvious then thats my excuse !!!

i am using a buffered reader to read through a csv file - but i dont want to add any empty spaces, as in

" "

the problem is - when i read the line i am checking if the string length is > 0, if so then add, however the string " " is of length 4 - even tho its empty

is there a better check i can do

thanks

+6  A: 
s.trim().isEmpty()
flybywire
+3  A: 

could trim the input :-)

trim()
      Returns a copy of the string, with leading and trailing whitespace omitted.

karl

Karl
+2  A: 

I usually prefer, StringUtils of commons-lang, or something like that. Actually, you require this kinda check every now and then, plus there are few variants and all are null-safe. Namely, StringUtils.isBlank(String str), StringUtils.isNotBlank(String str), StringUtils.isEmpty(String str), StringUtils.isNotEmpty(String str), StringUtils.trimToEmpty(String str), and StringUtils.trimToNull(String str).

Adeel Ansari
+1 for that - I use `StringUtils.defaultString()` (both the unary and binary versions) so often now that I feel it should be part of the language.
Andrzej Doyle