Want to write articles/new answer to be published in javapedia.net? Send in your article with your name to javatutorials2016@gmail.com!

Prev Next

Java / Using String

How to check if a string is not null and not empty in Java?

String.isEmpty() non-static method is introduced in Java SE 1.6 that evaluates the string object is empty or not given that the string object is not null otherwise it throws NullPointerException.

if(str != null && !str.isEmpty())

Be sure to use the left and the right conditions of && operator in the above order, as Java will not evaluate the right operand if the left operand is false, thus preventing the NullPointerException being thrown from str.isEmpty() if str is null.

To ignore the white space, use the trim method as shown below.

if(str != null && !str.trim().isEmpty())

Prior to Java SE 1.6, string.length method could be used to evaluate if the string is empty or not.

if(str != null && str.length()>0)

Invest now!!! Get Free equity stock!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.

More Related questions...

How do I iterate through every character in a String? (or) How to parse each character in a String object? toCharArray() Method How do I convert a char (primitive data type variable) to string in Java? Can we use String in switch case control statement? How do I split a pipe delimited Java String using split method? How do I create a Regular expression pattern into non-greedy in Java? Explain SimpleDateFormat class and its usage in Java. How to check if a string is not null and not empty in Java? How do I remove duplicate elements from a String Array in Java? How do I write a new line character to a file in Java? What is the maximum length of a String in Java? How many String objects be created by the snippet and where it is stored in Java? How do you convert a string object to character array in Java? Is string a wrapper class in Java? How many spaces does the Java String.trim() remove? Can we compare String and StringBuffer objects in Java? Which class is final:String, StringBuffer and StringBuider? What is the initial capacity of a StringBuilder object in Java? How to avoid NullPointerException when comparing two Strings in Java? Does substring method cause memory leak? what happens if beginIndex is equal to length of the String when calling substring(int beginIndex)? When substring method throws IndexOutOfBoundException? How to resolve substring memory leak issue in java version prior to JDK 1.7? Where String pool is located? What is the output of this statement?
System.out.println ("A"=="A");
What is the difference between appending a string to a StringBuilder and concatenating two strings with a + operator? How string concatenation operator (+) works in Java? When the concatenation of final string literal happen? is Java's SimpleDateFormat thread-safe?
Show more question and Answers...


Comments & Discussions