preface
When we say a string is empty, it’s just an empty array with no characters. Such as:
String a = "";
A can be called an empty string. Since String is stored as a char array underneath Java, the null String is represented as a char array
private final char value[] = new char[0];
But in actual work, we need to do some validation on the string, such as: whether null, whether null, whether to remove Spaces, line breaks, TAB characters and so on is not empty. We usually make these judgments through the framework’s utility classes, such as the Apache Commons JAR package. Here are two common string validation methods and their differences.
PS: in the process of writing project, recently found that a lot of places to be sentenced to empty operation, then can sometimes be invocation chain is longer, if use the if the else come to empty, empty code will be more light, this is not good for later maintenance, and we found empty may not consider a scene, this will result in a null pointer. I now strongly recommend using the third party JAR’s utility classes to do this. For example, take the value of a key from a Map, you can use MapUtils class; Use the StringUtils class for nulling strings; Nulling a collection using CollectionUtils and so on. These classes are available through the introduction of apache’s Commons package family.
isEmpty()
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank()
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
conclusion
Through the comparison of the above codes, we can see:
1. IsEmpty does not ignore the space parameter, based on whether it isEmpty and whether it exists.
2. IsBlank is a judgment on isEmpty (strings are Spaces, tabs, TAB) based on isEmpty. (more commonly used)
you can look at the following example to feel.
StringUtils.isEmpty("yyy") = false
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isBlank("yyy") = false
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
Read More:
- JavaScript summary: the difference between typeof and instanceof, and Object.prototype.toString() method
- The differences between the equals method in the string class and the equals method in the object class
- “Method does not override method from its superclass”
- Solve the problem that power view in Excel prompts activate method of oleobject class failed or activate method like oleobject is invalid
- Override the equals method and override the toString method
- On the intern () method of string class in Java
- Could not find method causes verifyerror, which in turn causes crash
- Method of ignoring case in Python string comparison
- Python’s direct method for solving linear equations (5) — square root method for solving linear equations
- Determine whether the list is empty isempty
- The method of getting shell command output in Python
- Tensorflow in function tf.Print Method of outputting intermediate value
- Java and Android commons codec usage and nosuch method error
- Java compareto() method
- Exception ignored in: bound method basesession__ del__ Of
- HBase checkandput() method
- Split screen display method in IntelliJ idea
- The intent solution cannot be updated in onrestart method
- Oops error location method in ARM kernel
- A convenient method to count the total number of query results in MySQL