, asked
Views : 169

Checking for Null or Empty or White Space Only String in Java.

I am doing a small project where I need to read data from a file and store into a database. But I need to eliminate null values. How can I test whether the given string is null? Please explain with an example.

1 Answer

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.


import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "tutorialspoint";
      // prints length of string
      System.out.println("length of string = " + str.length());
      // checks if the string is empty or not
      System.out.println("is this string empty? = " + str.isEmpty());


length of string = 14
is this string empty? = false

Related Questions