If a string is empty or not : String « Data Type « Java






If a string is empty or not

    
import org.apache.commons.lang.StringUtils;

public class Main {
  public static void main(String[] args) {
    String one = "";
    String two = "\t\r\n";
    String three = "     ";
    String four = null;
    String five = "four four two";

    // We can use StringUtils class for checking if a string is empty or not
    // using StringUtils.isBlank() method. This method will return true if
    // the tested string is empty, contains white space only or null.
    System.out.println("Is one empty? " + StringUtils.isBlank(one));
    System.out.println("Is two empty? " + StringUtils.isBlank(two));
    System.out.println("Is three empty? " + StringUtils.isBlank(three));
    System.out.println("Is four empty? " + StringUtils.isBlank(four));
    System.out.println("Is five empty? " + StringUtils.isBlank(five));

    // On the other side, the StringUtils.isNotBlank() methods complement
    // the previous method. It will check is a tested string is not empty.
    System.out.println("Is one not empty? " + StringUtils.isNotBlank(one));
    System.out.println("Is two not empty? " + StringUtils.isNotBlank(two));
    System.out.println("Is three not empty? " + StringUtils.isNotBlank(three));
    System.out.println("Is four not empty? " + StringUtils.isNotBlank(four));
    System.out.println("Is five not empty? " + StringUtils.isNotBlank(five));
  }
}
 

   
    
    
    
  








Related examples in the same category

1.Get String hash code
2.To extract Ascii codes from a String
3.LengthOf - show length of things
4.Show string escapesShow string escapes
5.Convert string to char array
6.See if Strings are shared in Java
7.Find text between two strings
8.Count word occurrences in a string
9.Check for an empty string
10.Remove leading and trailing space from String
11.Reverse a string
12.Put quotes around the given String if necessary.
13.Starts With Ignore Case
14.Repeat String
15.implements CharSequence