Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-07-2009
Member
 
Join Date: Apr 2008
Posts: 38
How to check java string not null or blank

Hi,

I need help with java string to check the string if its not null or blank. I want to check from may java code that the string is not null or blank.
I hope its a simple question & someone will definitely come up with the correct solution.
Thanks in advance.
Reply With Quote
  #2  
Old 29-07-2009
Member
 
Join Date: May 2008
Posts: 23
Re: How to check java string not null or blank

Null
Null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing. Internally it is just a binary 0, but in the high level Java language, it is a magic constant, quite distinct from zero, that internally could have any representation.

If you execute some code like this:

Code:
x.doSomething();
and x is null, you will raise a java.lang.NullPointerException. Note Null PointerException, not Null ReferenceException.

The difference between a null reference and a reference to an empty object, (e.g. a freshly minted default object, empty collection or empty Iterator) is a major headache in Java. When dealing with other people?s code I want to strangle them. Why won?t they tell me when a method might return null or empty? Why won?t they tell me if a method can?t accept a null or empty object?

Code:
if ( x != null )
   {
   x.doSomething();
   }
Reply With Quote
  #3  
Old 29-07-2009
Member
 
Join Date: May 2008
Posts: 35
Re: How to check java string not null or blank

Checking a sting object containing empty or null value

Apache has provided two methods isBlank() and isNotBlank() for checking the strings.

The class org.apache.commons.lang.StringUtils extends Object class and provides methods that operate on null safe string.

The methods used:
isBlank(String str): This method is used to check whether a string is empty ("") , null or whitespace.

isNotBlank(String str): This method is used to check whether a string is not empty (""), not null or not a whitespace .

There are two more methods which can be used:

isNotEmpty(String str): Use this method for checking a string whether it is not empty("") or not null.

isEmpty(String str): Use this method to check a string for its empty (" ) or null value.

Code:
 import org.apache.commons.lang.StringUtils;
 
public class CheckEmptyStringExample 
{    
    public static void main(String[] args)
    {
    String string1 = "";
    String string2 = "\t\r\n";
    String string3 = "     ";
    String string4 = null;
    String string5 = "Hi"; 
    System.out.println("\nString one is empty? " + 
StringUtils.isBlank(string1));
    System.out.println("String one is not empty? " +
 StringUtils.isNotBlank(string1));
    System.out.println("\nString two is empty? " + 
StringUtils.isBlank(string2));
    System.out.println("String two is not empty?" + 
StringUtils.isNotBlank(string2));
    System.out.println("\nString three is empty?" + 
StringUtils.isBlank(string3));
    System.out.println("String three is not empty?" +
StringUtils.isNotBlank(string3));
    System.out.println("\nString four is empty?" + 
StringUtils.isBlank(string4));
    System.out.println("String four is not empty?" + 
StringUtils.isNotBlank(string4));
    System.out.println("\nString five is empty?" + 
StringUtils.isBlank(string5));
    System.out.println("String five is not empty?" + 
StringUtils.isNotBlank(string5)); 
    }
}
Reply With Quote
  #4  
Old 30-07-2009
greenblue
 
Posts: n/a
Re: How to check java string not null or blank

I don't know what you want to do. If you're making report. Maybe you can try RAQ Report. It's a free java reporting tool. In RAQ Report, you can check java string not null or blank.
Reply With Quote
  #5  
Old 30-07-2009
Member
 
Join Date: Jul 2009
Posts: 11
Re: How to check java string not null or blank

Every application needs to have a util class

Just set up

public static bool IsNullOrEmpty(string stringVar)
bool b;
if(stringVar == null || stringVar.Length == 0)
{return b;}

and be done with it and stop whining about whidbey?

call it from anywhere:

if (!Util.IsNullOrEmpty(myString))
{myString="a good one"}

Hope it works!!!!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to check java string not null or blank"
Thread Thread Starter Forum Replies Last Post
How to replace a null string with blank cell in Microsoft Excel Eas!war MS Office Support 2 15-02-2012 05:52 PM
Problem with a null string in java Rily Software Development 4 12-08-2010 10:23 AM
How to Check Null Value in C#? sivaranjan Software Development 4 04-02-2010 04:49 AM
In Java Script how to Check Null Value? taher Software Development 5 04-02-2010 03:18 AM
SQL need to check null or empty string B_Hodge Software Development 3 18-06-2009 11:44 AM


All times are GMT +5.5. The time now is 07:38 PM.