Home Java Beginners Check Empty String
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Check Empty String

Advertisement
In this example we are checking a sting object containing empty or null value.

Check Empty String

     

In this example we are 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.

 The code of the program is given below:

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))
  }
}
 

The output of the program is given below:

C:\rajesh\kodejava>javac CheckEmptyStringExample.java
C:\rajesh\kodejava>java CheckEmptyStringExample
String one is empty? true
String one is not empty? false
String two is empty? true
String two is not empty?false
String three is empty?true
String three is not empty?false
String four is empty?true
String four is not empty?false
String five is empty?false
String five is not empty?true

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Ask Questions?    Discuss: Check Empty String   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
naren
January 17, 2012
JAVA cool.............

Awesum, gud one.........
DMCA.com