• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check whether a string is null or not

 
Narasimha Raju Naidu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi can any one tell how to check whether a string is null or not

regards
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(myString == null)...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(myString != null)...
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (myString!=null || myString.trim().length()>0){

That would crash anyway if the string is null. Use the "&&" operator instead.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Satou kurinosuke,
thanks for the correction.. yep.. it should have been && instead of ||.

Thanks,
Raghavan alias Saravanan M.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.6 introduced a new String method, isEmpty(). It "returns true if, and only if, length() is 0."

:roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic