Frequently used syntax

J

June Moore

if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...

Any views?
 
P

Patricia Shanahan

June said:
if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...

Any views?

Wouldn't you still need to code it as:

if (str != null && !str.isEmpty() )

to avoid dereferencing null pointers? Also, I'm not sure that your
particular interpretation of emptiness, which treats whitespace strings
as empty, is so dominant that it should be given the "isEmpty" identifier.

Patricia
 
R

Roedy Green

if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...

you can. I have methods for doing that as part of common11.

the confusion between null and "" shipwrecks many a program.
See http://mindprod.com/jgloss/void.html


Here are some:

/**
* Convert String to canonical standard form. null -> "". Trims
lead trail
* blanks.
*
* @param s
* String to be converted.
* @return String in canonical form.
*/
public final static String canonical ( String s )
{
if ( s == null )
return "";
else
return s.trim();
} // end canonical

/**
* Is this string empty?
*
* @param s
* String to be tested for emptiness.
* @return true if the string is null or equal to the "" null
string. or
* just blanks
*/
public final static boolean isEmpty ( String s )
{
return ( s == null ) ? true : s.trim().length() == 0;
} // end isEmpty
 
T

Thomas Hawtin

Gijs said:
You could use something like String.valueOf(str).trim().length()

You could, but you'd get the wrong answer for null.
[ June Moore wrote: ]
if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )

According the JavaDocs for java.lang.String on my machine:

isEmpty

public boolean isEmpty()

Returns true if, and only if, length() is 0.

Returns:
true if length() is 0, otherwise false
Since:
1.6

Tom Hawtin
 
K

karlheinz klingbeil

June Moore schrub am Freitag, 2. September 2005 08:00
folgendes:
if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...
Yes it would, but...

istEmpty() is a Method of an existing Instance of
String. If the String is null, it is nonexistent, and
therefore isEmpty() will throw a
Nullpointer-Exception.

So the first line really is IMHO the best solution to
check for a null-string or a string which only
contains whitespace.
 
M

megagurka

Roedy Green skrev:
public final static boolean isEmpty ( String s )
{
return ( s == null ) ? true : s.trim().length() == 0;

Which can be simplified to:

return s == null || s.trim().length() == 0;

/JN
 
M

Malte

June said:
if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...

Any views?

Sure, but who hasn't implemented this already in own toolbox?
 
J

Jim Janney

June Moore said:
if ( str != null && str.trim().length() > 0 )
...

Wouldn't it be neat if JDK will just say

if ( !str.isEmpty() )
...

Any views?

Use Jakarta Commons and write

if (!StringUtils.isEmpty(str))

It also allows

if (StringUtils.equals(str1, str2))

and many other handy functions. You can't expect the JDK to include
everything :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top