Use StringUtils.isBlank()
. This method will return true if it is
supplied with an empty, zero length, or whitespace-only string. This
method gracefully handles a null
input by
returning true. The following example demonstrates isBlank( )
with four strings:
String test = ""; String test2 = " "; String test3 = null; String test4 = "Test"; System.out.println( "test blank? " + StringUtils.isBlank( test ) ); System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) ); System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) ); System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );
All four strings are tested, and the following output is produced:
test blank? true test2 blank? true test3 blank? true test4 blank? false
Checking for nonblank strings is just as easy;
StringUtils.isNotBlank( )
will return the
compliment of isBlank( )
. If a string is empty,
contains only whitespace, or is null
, the
StringUtils.isNotBlank( )
method will return
false. This method comes in handy when a process expects a certain
string to have content, and it gracefully handles a
null
input by returning false:
String test = " "; String test2 = "Test"; System.out.println( "test is not blank? " + StringUtils.isNotBlank( test ) ); System.out.println( "test2 is not blank? " + StringUtils.isNotBlank( test2 ) );
This produces the following output, which shows that a string containing only whitespace is considered blank:
test is not blank? false test2 is not blank? true
Another method to test for an empty string is to
“trim” a string to
null
if it contains only whitespace, and then test
to see if the trimmed result is null
. Use
StringUtils.trimToNull( )
to transform empty
strings to null
. This method handles a
null
parameter value by returning
null
.
String test1 = " "; String test2 = "Test"; String test3 = null; System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) ); System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) ); System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );
This produces the following output, which shows that a string
containing only whitespace is trimmed to null
:
test1 trimToNull: null test2 trimToNull: Test test3 trimToNull: null
These three methods are laughably simple, but they will serve to
reduce the amount of complexity in a system that needs to test
strings. Use the methods from this recipe to avoid writing code to
test for a null
, and check the length of a string:
if( variable != null && variable.length( ) > 0 && !variable.trim( ).equals("") ) { // Do something }
The code above can be rewritten with one call to
StringUtils.isNotBlank( )
:
if( StringUtils.isNotBlank( variable ) ) { // Do something }
It is good practice to avoid using null
whenever
possible; code that uses null
to signify emptiness
or an error condition is harder to maintain and more likely to throw
a nasty NullPointerException
. Worse yet are
methods that return a null
whenever a problem
occurs or an exception is thrown. While trimToNull()
can accomplish the goal, it does increase the occurrence
of null
s in code;
use isBlank( )
and isNotBlank( )
to excise dangerous
null
s from your code.
3.133.122.68