aspose file tools*
The moose likes Beginning Java and the fly likes how to check whether a string is null or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check whether a string is null or not" Watch "how to check whether a string is null or not" New topic
Author

how to check whether a string is null or not

Narasimha Raju Naidu
Ranch Hand

Joined: Mar 23, 2007
Posts: 37
hi can any one tell how to check whether a string is null or not

regards
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

if(myString == null)...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

if(myString != null)...


[My Blog]
All roads lead to JavaRanch
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
You can additionally check whether the String object has blank value or not

if(myString==null || ("").equals(myString)){
//Code to assign a value to string
}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

you can also include this way of checking..

if (myString!=null || myString.trim().length()>0){

//do whatever you want to do with myString's value
System.out.println("valid and non-empty string");
}

Hope this helps.
[ April 16, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40058
    
  28
Posted by Raghavan Muthu
. . . myString.trim().length()>0 . . .
That won't only detect a blank (or more precisely empty) String, but also a String containing whitespace only, which is something different. You can use a similar technique to detect an empty String:-CR
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

hello CR,

thanks for the notification. yes.. if the requirement is to deal with only a NON-EMPTY string (blankspaces not to be considered), trim() can be used before finding its length.

trim() is used to get the string by omitting the whitespaces at both the ends
[ April 16, 2007: Message edited by: Raghavan Muthu ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

if (myString!=null || myString.trim().length()>0){

That would crash anyway if the string is null. Use the "&&" operator instead.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

hi Satou kurinosuke,
thanks for the correction.. yep.. it should have been && instead of ||.

Thanks,
Raghavan alias Saravanan M.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Java 1.6 introduced a new String method, isEmpty(). It "returns true if, and only if, length() is 0."

:roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check whether a string is null or not