Sunday, July 27, 2008
StringUtils.isBlank() method check to see is the string contains only whitespace characters, empty or has a null value. If these condition is true that the string considered blank.
There's also a StringUtils.isEmpty(), only these method doesn't not check for whitespaces only string. For checking the oposite condition there are StringUtils.isNotBlank() and StringUtils.isNotEmpty().
Using this methods we can avoid repeating the code for checking empty string which can include more code to type then using these handy method.
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.StringUtils;
public class CheckEmptyString {
public static void main(String[] args) {
String var1 = null;
String var2 = "";
String var3 = " \t\t\t";
String var4 = "Hello World";
System.out.println("var1 is blank? = " + StringUtils.isBlank(var1));
System.out.println("var2 is blank? = " + StringUtils.isBlank(var2));
System.out.println("var3 is blank? = " + StringUtils.isBlank(var3));
System.out.println("var4 is blank? = " + StringUtils.isBlank(var4));
System.out.println("var1 is not blank? = " + StringUtils.isNotBlank(var1));
System.out.println("var2 is not blank? = " + StringUtils.isNotBlank(var2));
System.out.println("var3 is not blank? = " + StringUtils.isNotBlank(var3));
System.out.println("var4 is not blank? = " + StringUtils.isNotBlank(var4));
System.out.println("var1 is empty? = " + StringUtils.isEmpty(var1));
System.out.println("var2 is empty? = " + StringUtils.isEmpty(var2));
System.out.println("var3 is empty? = " + StringUtils.isEmpty(var3));
System.out.println("var4 is empty? = " + StringUtils.isEmpty(var4));
System.out.println("var1 is not empty? = " + StringUtils.isNotEmpty(var1));
System.out.println("var2 is not empty? = " + StringUtils.isNotEmpty(var2));
System.out.println("var3 is not empty? = " + StringUtils.isNotEmpty(var3));
System.out.println("var4 is not empty? = " + StringUtils.isNotEmpty(var4));
}
}
How do I check for an empty string?
Bookmark this example!
Category: Commons Lang, viewed: 2858 time(s).
StringUtils.isBlank() method check to see is the string contains only whitespace characters, empty or has a null value. If these condition is true that the string considered blank.
There's also a StringUtils.isEmpty(), only these method doesn't not check for whitespaces only string. For checking the oposite condition there are StringUtils.isNotBlank() and StringUtils.isNotEmpty().
Using this methods we can avoid repeating the code for checking empty string which can include more code to type then using these handy method.
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.StringUtils;
public class CheckEmptyString {
public static void main(String[] args) {
String var1 = null;
String var2 = "";
String var3 = " \t\t\t";
String var4 = "Hello World";
System.out.println("var1 is blank? = " + StringUtils.isBlank(var1));
System.out.println("var2 is blank? = " + StringUtils.isBlank(var2));
System.out.println("var3 is blank? = " + StringUtils.isBlank(var3));
System.out.println("var4 is blank? = " + StringUtils.isBlank(var4));
System.out.println("var1 is not blank? = " + StringUtils.isNotBlank(var1));
System.out.println("var2 is not blank? = " + StringUtils.isNotBlank(var2));
System.out.println("var3 is not blank? = " + StringUtils.isNotBlank(var3));
System.out.println("var4 is not blank? = " + StringUtils.isNotBlank(var4));
System.out.println("var1 is empty? = " + StringUtils.isEmpty(var1));
System.out.println("var2 is empty? = " + StringUtils.isEmpty(var2));
System.out.println("var3 is empty? = " + StringUtils.isEmpty(var3));
System.out.println("var4 is empty? = " + StringUtils.isEmpty(var4));
System.out.println("var1 is not empty? = " + StringUtils.isNotEmpty(var1));
System.out.println("var2 is not empty? = " + StringUtils.isNotEmpty(var2));
System.out.println("var3 is not empty? = " + StringUtils.isNotEmpty(var3));
System.out.println("var4 is not empty? = " + StringUtils.isNotEmpty(var4));
}
}
The result of our program are:
var1 is blank? = true
var2 is blank? = true
var3 is blank? = true
var4 is blank? = false
var1 is not blank? = false
var2 is not blank? = false
var3 is not blank? = false
var4 is not blank? = true
var1 is empty? = true
var2 is empty? = true
var3 is empty? = false
var4 is empty? = false
var1 is not empty? = false
var2 is not empty? = false
var3 is not empty? = true
var4 is not empty? = true
There's also a StringUtils.isEmpty(), only these method doesn't not check for whitespaces only string. For checking the oposite condition there are StringUtils.isNotBlank() and StringUtils.isNotEmpty().
Using this methods we can avoid repeating the code for checking empty string which can include more code to type then using these handy method.
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.StringUtils;
public class CheckEmptyString {
public static void main(String[] args) {
String var1 = null;
String var2 = "";
String var3 = " \t\t\t";
String var4 = "Hello World";
System.out.println("var1 is blank? = " + StringUtils.isBlank(var1));
System.out.println("var2 is blank? = " + StringUtils.isBlank(var2));
System.out.println("var3 is blank? = " + StringUtils.isBlank(var3));
System.out.println("var4 is blank? = " + StringUtils.isBlank(var4));
System.out.println("var1 is not blank? = " + StringUtils.isNotBlank(var1));
System.out.println("var2 is not blank? = " + StringUtils.isNotBlank(var2));
System.out.println("var3 is not blank? = " + StringUtils.isNotBlank(var3));
System.out.println("var4 is not blank? = " + StringUtils.isNotBlank(var4));
System.out.println("var1 is empty? = " + StringUtils.isEmpty(var1));
System.out.println("var2 is empty? = " + StringUtils.isEmpty(var2));
System.out.println("var3 is empty? = " + StringUtils.isEmpty(var3));
System.out.println("var4 is empty? = " + StringUtils.isEmpty(var4));
System.out.println("var1 is not empty? = " + StringUtils.isNotEmpty(var1));
System.out.println("var2 is not empty? = " + StringUtils.isNotEmpty(var2));
System.out.println("var3 is not empty? = " + StringUtils.isNotEmpty(var3));
System.out.println("var4 is not empty? = " + StringUtils.isNotEmpty(var4));
}
}
How do I check for an empty string?
Bookmark this example!
Category: Commons Lang, viewed: 2858 time(s).
StringUtils.isBlank() method check to see is the string contains only whitespace characters, empty or has a null value. If these condition is true that the string considered blank.
There's also a StringUtils.isEmpty(), only these method doesn't not check for whitespaces only string. For checking the oposite condition there are StringUtils.isNotBlank() and StringUtils.isNotEmpty().
Using this methods we can avoid repeating the code for checking empty string which can include more code to type then using these handy method.
package org.kodejava.example.commons.lang;
import org.apache.commons.lang.StringUtils;
public class CheckEmptyString {
public static void main(String[] args) {
String var1 = null;
String var2 = "";
String var3 = " \t\t\t";
String var4 = "Hello World";
System.out.println("var1 is blank? = " + StringUtils.isBlank(var1));
System.out.println("var2 is blank? = " + StringUtils.isBlank(var2));
System.out.println("var3 is blank? = " + StringUtils.isBlank(var3));
System.out.println("var4 is blank? = " + StringUtils.isBlank(var4));
System.out.println("var1 is not blank? = " + StringUtils.isNotBlank(var1));
System.out.println("var2 is not blank? = " + StringUtils.isNotBlank(var2));
System.out.println("var3 is not blank? = " + StringUtils.isNotBlank(var3));
System.out.println("var4 is not blank? = " + StringUtils.isNotBlank(var4));
System.out.println("var1 is empty? = " + StringUtils.isEmpty(var1));
System.out.println("var2 is empty? = " + StringUtils.isEmpty(var2));
System.out.println("var3 is empty? = " + StringUtils.isEmpty(var3));
System.out.println("var4 is empty? = " + StringUtils.isEmpty(var4));
System.out.println("var1 is not empty? = " + StringUtils.isNotEmpty(var1));
System.out.println("var2 is not empty? = " + StringUtils.isNotEmpty(var2));
System.out.println("var3 is not empty? = " + StringUtils.isNotEmpty(var3));
System.out.println("var4 is not empty? = " + StringUtils.isNotEmpty(var4));
}
}
The result of our program are:
var1 is blank? = true
var2 is blank? = true
var3 is blank? = true
var4 is blank? = false
var1 is not blank? = false
var2 is not blank? = false
var3 is not blank? = false
var4 is not blank? = true
var1 is empty? = true
var2 is empty? = true
var3 is empty? = false
var4 is empty? = false
var1 is not empty? = false
var2 is not empty? = false
var3 is not empty? = true
var4 is not empty? = true
Subscribe to:
Post Comments (Atom)
SUBSCRIBE VIA eMAIL
Recent Posts
Archives
- December 2008 (5)
- November 2008 (15)
- October 2008 (17)
- September 2008 (9)
- August 2008 (12)
- July 2008 (19)
- June 2008 (22)
- May 2008 (17)
- April 2008 (2)
Categories
- java.sql (19)
- Examples (18)
- INTRODUCTION (9)
- JAVA APPLET (9)
- java.awt (8)
- java.net (8)
- java.beans (7)
- JAVA String Utility (6)
- Arrays (4)
- java.math (3)
- java.util.regex (3)
- Sort (2)
- Swing (2)
- java.security (2)
- java.util.zip (2)
- Catching Exceptions (1)
- Classes and Objects (1)
- Core Java Programs-part 1 (1)
- Core Java Programs-part 2 (1)
- Core Java Programs-part3 (1)
- Criticism of Java programming language (1)
- Falling Letters (1)
- File I/O and Streams (1)
- Fun With Letters and Words (1)
- Get current working directory (1)
- How do I convert String to Date object? (1)
- How do I convert string into InputStream? (1)
- How do i calculate directory size? (1)
- How to make executable jar files in JDK1.3.1? (1)
- Interfaces (1)
- JAVA Date Utility (1)
- Java Arithmetic Operators (1)
- Java Assignment Operators (1)
- Java Boolean Operators (1)
- Java Command Line Arguments (1)
- Java Comments (1)
- Java Conditional Operators (1)
- Java Data and Variables (1)
- Java Hello World Program (1)
- Java If-Else Statement (1)
- Java Increment and Decrement Operators (1)
- Java Loops (while (1)
- Java Relational Operators (1)
- Java Variables and Arithmetic Expressions (1)
- Java Virtual Machine (1)
- Know the current position of cursor (1)
- Letters (1)
- Methods (Includes Recursive Methods) (1)
- Rotating Lines (1)
- Send an email with attachment (1)
- Use for..each in Java (1)
- What is Autoboxing? (1)
- Write text file (1)
- connection to database (1)
- do-while and for loops) (1)


0 comments:
Post a Comment