Differences between isNotEmpty and isNotBlank in StringUtils

Source: Internet
Author: User

The operation object of the StringUtils method is java. lang. string-type objects are complementary to the String-type operation method provided by JDK, and are null safe (that is, if the input parameter String is null, NullPointerException is not thrown, but corresponding processing is performed, for example, if the input is null, the return value is also null. You can view the source code ).


In addition to the constringutils, there are more than 130 methods in StringUtils and they are all static, so we can call StringUtils. xxx () in this way ()


1. public static boolean isEmpty (String str)
Determines whether a string is null. If it is null, the standard is str = null or str. length () = 0.
The following is an example of StringUtils determining whether it is null:
StringUtils. isEmpty (null) = true
StringUtils. isEmpty ("") = true
StringUtils. isEmpty ("") = false // note that non-empty spaces are processed in StringUtils.
StringUtils. isEmpty ("") = false
StringUtils. isEmpty ("bob") = false
StringUtils. isEmpty ("bob") = false


2. public static boolean isNotEmpty (String str)
Determines whether a string is not null. It is equal! IsEmpty (String str)
The following is an example:
StringUtils. isNotEmpty (null) = false
StringUtils. isNotEmpty ("") = false
StringUtils. isNotEmpty ("") = true
StringUtils. isNotEmpty ("") = true
StringUtils. isNotEmpty ("bob") = true
StringUtils. isNotEmpty ("bob") = true


3. public static boolean isBlank (String str)
Determines whether a string is null or has a length of 0 or is composed of whitespace.
The following is an example:
StringUtils. isBlank (null) = true
StringUtils. isBlank ("") = true
StringUtils. isBlank ("") = true
StringUtils. isBlank ("") = true
StringUtils. isBlank ("\ t \ n \ f \ r") = true // blank characters are recognized for tabs, line breaks, page breaks, and carriage returns. StringUtils. isBlank ()
StringUtils. isBlank ("\ B") = false // "\ B" is the word boundary.
StringUtils. isBlank ("bob") = false
StringUtils. isBlank ("bob") = false


4. public static boolean isNotBlank (String str)
Determines whether a string is not empty and its length is not 0. It cannot be composed of whitespace. It is equal! IsBlank (String str)
The following is an example:
StringUtils. isNotBlank (null) = false
StringUtils. isNotBlank ("") = false
StringUtils. isNotBlank ("") = false
StringUtils. isNotBlank ("") = false
StringUtils. isNotBlank ("\ t \ n \ f \ r") = false
StringUtils. isNotBlank ("\ B") = true
StringUtils. isNotBlank ("bob") = true
StringUtils. isNotBlank ("bob") = true


Other methods:
5. public static String trim (String str)
Removes the control character (char <= 32) at both ends of the string. If the input is null, null is returned.
The following is an example:
StringUtils. trim (null) = null
StringUtils. trim ("") = ""
StringUtils. trim ("") = ""
StringUtils. trim ("\ B \ t \ n \ f \ r") = ""
StringUtils. trim ("\ n \ tss \ B") = "ss"
StringUtils. trim ("d dd") = "d dd"
StringUtils. trim ("dd") = "dd"
StringUtils. trim ("dd") = "dd"


6. public static String trimToNull (String str)
Removes the control character (char <= 32) at both ends of the string. If it is null or "", null is returned.
The following is an example:
StringUtils. trimToNull (null) = null
StringUtils. trimToNull ("") = null
StringUtils. trimToNull ("") = null
StringUtils. trimToNull ("\ B \ t \ n \ f \ r") = null
StringUtils. trimToNull ("\ n \ tss \ B") = "ss"
StringUtils. trimToNull ("d dd") = "d dd"
StringUtils. trimToNull ("dd") = "dd"
StringUtils. trimToNull ("dd") = "dd"


7. public static String trimToEmpty (String str)
Removes the control character (char <= 32) at both ends of the string. If it is null or "", "" is returned ""
The following is an example:
StringUtils. trimToEmpty (null) = ""
StringUtils. trimToEmpty ("") = ""
StringUtils. trimToEmpty ("") = ""
StringUtils. trimToEmpty ("\ B \ t \ n \ f \ r") = ""
StringUtils. trimToEmpty ("\ n \ tss \ B") = "ss"
StringUtils. trimToEmpty ("d dd") = "d dd"
StringUtils. trimToEmpty ("dd") = "dd"
StringUtils. trimToEmpty ("dd") = "dd"


8. public static String strip (String str)
Removes the whitespace at both ends of the string. If the input is null, null is returned.
The following is an example (note the difference with trim ):
StringUtils. strip (null) = null
StringUtils. strip ("") = ""
StringUtils. strip ("") = ""
StringUtils. strip ("\ B \ t \ n \ f \ r") = "\ B"
StringUtils. strip ("\ n \ tss \ B") = "ss \ B"
StringUtils. strip ("d dd") = "d dd"
StringUtils. strip ("dd") = "dd"
StringUtils. strip ("dd") = "dd"


9. public static String stripToNull (String str)
Removes the whitespace at both ends of the string. If it is null or "", null is returned.
The following is an example (note the difference between trimToNull () and trimToNull ):
StringUtils. stripToNull (null) = null
StringUtils. stripToNull ("") = null
StringUtils. stripToNull ("") = null
StringUtils. stripToNull ("\ B \ t \ n \ f \ r") = "\ B"
StringUtils. stripToNull ("\ n \ tss \ B") = "ss \ B"
StringUtils. stripToNull ("d dd") = "d dd"
StringUtils. stripToNull ("dd") = "dd"
StringUtils. stripToNull ("dd") = "dd"


10. public static String stripToEmpty (String str)
Remove the whitespace at both ends of the string. If it is null or "", return ""
The following is an example (note the difference between trimToEmpty () and trimToEmpty ):
StringUtils. stripToNull (null) = ""
StringUtils. stripToNull ("") = ""
StringUtils. stripToNull ("") = ""
StringUtils. stripToNull ("\ B \ t \ n \ f \ r") = "\ B"
StringUtils. stripToNull ("\ n \ tss \ B") = "ss \ B"
StringUtils. stripToNull ("d dd") = "d dd"
StringUtils. stripToNull ("dd") = "dd"
StringUtils. stripToNull ("dd") = "dd"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.