Monday, August 10, 2015

[Java] Check if string is empty


Sample utility class to check if String object is empty or null.



public class EmptyStringCheck {


   public static boolean isStrNull(String str)
   {
      boolean isNull = false;

      if (str != null)
      {
         isNull = false;
      }
      else
      {
         isNull = true;
      }

      return isNull;

   }//End method


   public static boolean isStrEmpty(String str)
   {
      boolean isEmpty = false;

      if (isStrNull(str))
      {
         isEmpty = true;
      }
      else
      {
         if (!("".equalsIgnoreCase(str)))
 {
    isEmpty = false;
 }
 else
 {
    isEmpty = true;
 }
      }

      return isEmpty;

   }//End method


   //Main method.
   public static void main(String[] args) {

      String str = "Test";
      String str1 = "";

      System.out.println(">>Is empty :::" + isStrEmpty(str));
      System.out.println(">>Is empty ::: " + isStrEmpty(str1));

      //Or
      System.out.println(">>Is empty :::" + str.isEmpty());
      System.out.println(">>Is empty ::: " + str1.isEmpty());

   }//End main method




}//End class

No comments:

Post a Comment