How do I check if a string is empty or not?
Category: Commons Lang, viewed: 2306 time(s).
package org.kodejava.example.commons.lang; import org.apache.commons.lang.StringUtils; public class EmptyStringCheckExample { public static void main(String[] args) { // Create some variable to hold some string that empty, contains only // white spaces and words. 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)); } } |
Here is the result:
Is one empty? true Is two empty? true Is three empty? true Is four empty? true Is five empty? false Is one not empty? false Is two not empty? false Is three not empty? false Is four not empty? false Is five not empty? true
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I find text between two strings?
- How do I check for an empty string?
- How do I get the nearest hour, minute, second of a date?
- How do I format date and time using DateFormatUtils class?
- How do I find items in an array?
- How do I use CompareToBuilder class?
- How do I use ReflectionToStringBuilder class?
- How do I convert array of object to array of primitive?
- How do I convert an array to a Map?
- How do I reverse array elements order?
- How do I count word occurrences in a string?
- How do I reverse a string, words or sentences?
- How do I implement equals method using commons-lang?
- How do I implement hashCode using commons-lang?
- How do I convert array of primitives into array of objects?
- How do I use Jakarta Commons ToStringBuilder?
Most Viewed Examples
Latest Code Examples
Categories
100 Top & Latest
Latest Jobs
Blog Entries
Forums Entries
- Google Chrome
- Re: Importing and scanning the text
- Re: What Java books have you read?
- Re: How to create exe file
- How to create exe file
- Re: What Java books have you read?
- Infix expression
- Importing and scanning the text
- Re: I want to reduce the size of a JPG image
- MOVED: I want to reduce the size of a JPG image
