• Post Reply Bookmark Topic Watch Topic
  • New Topic

String testing for null, empty  RSS feed

 
Nigel Hoath
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been beavering away with Java for a few months. But as with all languages the String implementation looks designed to trip up even experienced programmers.

My current development gets data from various sources outside my control. When I get a string I want to test if it is empty/null/or whatever. Simple enough one thinks.

But if you search the internet you see everone seems to have a slightly different approach. So what is the best way of determining that a string is not useful to you?

I've had success with this



But I've seen people using methods - not necessarily of String (e.g equals, empty) and regular expressions.

What is the best approach to this considering coding efficiency and/or processing efficiency (accepting you'd have to be processing a lot of strings for the latter to be an issue).

Any thoughts very welcome as I'd like to standardise on an approach. Cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you find lacking in the approach you've already pointed out?
 
Nigel Hoath
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What do you find lacking in the approach you've already pointed out?


Hi bear. Nothing so far but early on I had imagined testing null would be sufficient. It isn't as I've found zero length strings that are not null.

So as I process data from alien sources on several platforms I cannot predict what may arrive. And I'd like to not have to keep going back fixing up. So if what I have now is good then great but just looking for confirmation.

Cheers





 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Hoath wrote:

Commonly used alternatives to this are Apache Commons StringUtils and Guava Strings

Read the API documentation to see how each of these behave as they may or may not be exactly what you want.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Hoath wrote: . . .
But I've seen people using methods - not necessarily of String (e.g equals, empty) and regular expressions.
. . .
Not empty bur isEmpty
If you look on that link, you see it says “Since 1.6” and Java6 came out in late 2006 if I remember correctly. That suggests there were ten years of complaints aboiut having to use if length()==0 why can't we have an isEmpty() method

But as you have been told, your formula will reliably identify no String at all (=null) and empty Strings. There is a difference between a 0‑length String and null. The two are mutually incompatible so you would have to test for both. There are all sorts of other kinds of String validation you can consider, but your approach is good. You can use regular expressions to examine Strings for particular patterns, which is a very powerful technique, but does require lots of thought to get to work reliably.
 
Nigel Hoath
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well many thanks folks for all the replies. It looks like I can rely on my current approach.

Campbell you make a good point about out of date info referring to previous versions. That may well explain some of the convoluted solutions to what seems a simple case.

Onwards and upwards, Cheers
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yoiu're welcome
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another tip for dealing with Strings if you need to check for a specific value. I see this a lot:
The purpose being to avoid a possible NullPointerException when calling .equals().

Here's a tip to avoid the null check. The String literal will never be null so no need to check for it:
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately neither of those techniques will pick up null/empty.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. Perhaps I went a little off topic, but I did say it was a tip for when you need to check that a String variable contains a specific value.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!