This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient Way to Check Empty String  RSS feed

 
ppravin rane
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We use (string != null || string.trim().length() == 0) to check if string is empty. This way is said to be inefficient since string.trim() creates a new string object just to check if string contains any non-whitespace character.

It is recommended to 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.

Can you please let me know what would be more expensive
1. Creating new string object
2. Calling a method which actually iterate through string and checks for non-whitespace character

Regards
~Pravin
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppravin,

welcome to the JavaRanch

Why not just use String.isEmpty() ??

Marco
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marco Ehrentreich:
Why not just use String.isEmpty() ?


Because the original poster wants to consider a string that only contains whitespace to be "empty".

You could use a precompiled regex to check for all-whitespace strings efficiently. I don't know whether this would be faster or slower than other methods.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll weigh in with the Standard Optimization Answer: "It probably doesn't matter".
Compared to all the work going on in a non-trivial program and considering that modern VM's handle short-term object creation better than they used to, creating a single extra String object probably will not have an impact.
That said, if your program pumps millions of Strings through this code, there is bound to be some impact.
Have a look at the Java Platform Performance book. It details how to benchmark code and compare different implementations to determine which is best.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first optimization which occurs to me is to handle the "short circuit" case where length() == 0 right after the check for null.

Bill
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess: I'll weigh in with the Standard Optimization Answer: "It probably doesn't matter".


I agree with Joe, this is 99.99% likely to be a premature optimization.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, go look at the implementation of String.trim() -- it returns the original String, unaltered, most of the time. Only if trim() actually needs to return a different String does it actually create a new one -- i.e., only if there was actually trailing or leading whitespace. I'd say the easy expression above is going to be good enough in virtually any circumstance.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally agree that this is likely premature optimization.

But for the case that it is not, the most efficient way also depends on the typical usage patterns of the code. For example, if you know that 99% of the Strings you test will consist of exactly one white space, the "short circuit" optimization proposed by Bill - while making sense in the general case - would probably *decrease* performance.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My personal favorite method to do this is:

StringUtils.isBlank(str);

// or, depending on situation

StringUtils.isNonBlank(str);


I trust people from Jakarta commons lang to optimize this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!