determining if a string is null

G

Gary N.

Is there an easy way to determine if a string is null?

String a = null;
a.equals(""); // doesn't work

FAQ or suggestions welcome.

Thanks!

Gary
 
A

Anand Gopinath

+Is there an easy way to determine if a string is null?

Sure there is. if( a == null ) will tell you if a is null or not.
The "" is not null, its a null string ( meaning its a string with length
0 ).

+String a = null;
+a.equals(""); // doesn't work

Anand Gopinath
 
V

VisionSet

Gary N. said:
Is there an easy way to determine if a string is null?

String a = null;
a.equals(""); // doesn't work

FAQ or suggestions welcome.

"" is an empty String it is a String object just one with zero characters
a null String is a lack of a String Object altogether!

String a = null;

if(a==null) // String a is null
 
B

biswa

String strExample = null;
...
...
if(strExample == null){} // to check if it is null

to check if it is blank --
if(strExample != null && strExample.trim().length() ==0)//empty string.
{
}

BTW please check the String class for other methods.

biswa.
 
A

Anonymous

If you are trying to determine if it is empty (as opposed to null),
then:

if (a.length() == 0) {}



String strExample = null;
..
..
if(strExample == null){} // to check if it is null

to check if it is blank --
if(strExample != null && strExample.trim().length() ==0)//empty string.
{
}

BTW please check the String class for other methods.

biswa.


Windward Reports -- http://www.WindwardReports.com
DefendTek -- http://www.DefendTek.com
Page 2 Stage -- http://www.Page2Stage.com
Enemy Nations -- http://www.EnemyNations.com
me -- http://dave.thielen.com
Barbie Science Fair -- http://www.BarbieScienceFair.info
Hillary Clinton -- http://www.HillaryIn2004.org
(yes I have lots of links)
 
C

Chris Smith

Anand said:
+Is there an easy way to determine if a string is null?

Sure there is. if( a == null ) will tell you if a is null or not.
The "" is not null, its a null string ( meaning its a string with length
0 ).

To avoid confusion, most people call that the "empty string", not the
"null string"> I'd be confused if you used the latter term talking to
me, anyway.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Gary said:
Is there an easy way to determine if a string is null?

String a = null;
a.equals(""); // doesn't work

Just to add to your other replies... you're using terminology in a
dangerous way here. In the code above, you can't really check to see if
a String is null, because there isn't a String there at all. Instead,
you have a reference, and that reference is *capable* of pointing to a
String... but the whole meaning of 'null' is that there is no String
there at all. (Furthermore, if there were a String, it would
meaningless to say that the String "is null". Only references can be
null.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top