How to check whether a given string is empty or not in Java?

Learn: How to check whether a given string is empty or not using a java program? In this program we will use String.isEmpty() method to check given string is empty or not?

String.isEmpty()

It is a predefined (built-in) method of String class in Java, it returns "true" if given string is empty otherwise it returns "false".

Consider the program:

Given string (Input string) and we have to whether it is empty string or not?

/*Java Program to check whether string is empty or not*/

public class JavaisEmptyPrg
{
	public static void main(String args[])
	{
		String str1="www.includehelp.com";
		String str2="";

		if(str1.isEmpty()==true)
		System.out.println("Str1 is an empty string.");
		else
		System.out.println("Str1 is not an empty string.");

		if(str2.isEmpty()==true)
			System.out.println("Str2 is an empty string.");
		else
			System.out.println("Str2 is not an empty string.");
	}
}

Output

Complie: javac JavaisEmptyPrg.java
Run: java JavaisEmptyPrg

Output

Str1 is not an empty string.
Str2 is an empty string.

Java String Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.