• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Efficient Way to Check Empty String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ppravin,

welcome to the JavaRanch

Why not just use String.isEmpty() ??

Marco
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic