isEmpty系列

StringUtils.isEmpty()

是否为空. 可以看到 “ “ 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(“ “)=false

  1. StringUtils.isEmpty(null) = true
  2. StringUtils.isEmpty("") = true
  3. StringUtils.isEmpty(" ") = false
  4. StringUtils.isEmpty("bob") = false
  5. StringUtils.isEmpty(" bob ") = false
  1. /**
  2. *
  3. * <p>NOTE:This method changed in Lang version 2.0.
  4. * It no longer trims the CharSequence.
  5. * That functionality is available in isBlank().</p>
  6. *
  7. * @paramcs the CharSequence to check, may be null
  8. * @return{@codetrue} if the CharSequence is empty or null
  9. * @since3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
  10. */
  11. publicstaticbooleanisEmpty(finalCharSequence cs){
  12. returncs ==null|| cs.length() ==0;
  13. }

StringUtils.isNotEmpty()

相当于不为空 , = !isEmpty()

  1. publicstaticbooleanisNotEmpty(finalCharSequence cs){
  2. return!isEmpty(cs);
  3. }

StringUtils.isAnyEmpty()

是否有一个为空,只有一个为空,就为true.

  1. StringUtils.isAnyEmpty(null) =true
  2. StringUtils.isAnyEmpty(null,"foo") =true
  3. StringUtils.isAnyEmpty("","bar") =true
  4. StringUtils.isAnyEmpty("bob","") =true
  5. StringUtils.isAnyEmpty("bob ",null) =true
  6. StringUtils.isAnyEmpty(" ","bar") =false
  7. StringUtils.isAnyEmpty("foo","bar") =false
  8. /**
  9. * @paramcss the CharSequences to check, may be null or empty
  10. * @return{@codetrue} if any of the CharSequences are empty or null
  11. * @since3.2
  12. */
  13. publicstaticbooleanisAnyEmpty(finalCharSequence... css){
  14. if(ArrayUtils.isEmpty(css)) {
  15. returntrue;
  16. }
  17. for(finalCharSequence cs : css){
  18. if(isEmpty(cs)) {
  19. returntrue;
  20. }
  21. }
  22. returnfalse;
  23. }

StringUtils.isNoneEmpty()

相当于!isAnyEmpty(css) , 必须所有的值都不为空才返回true

  1. /**
  2. * <p>Checks if none of the CharSequences are empty ("") or null.</p>
  3. *
  4. * <pre>
  5. * StringUtils.isNoneEmpty(null) = false
  6. * StringUtils.isNoneEmpty(null, "foo") = false
  7. * StringUtils.isNoneEmpty("", "bar") = false
  8. * StringUtils.isNoneEmpty("bob", "") = false
  9. * StringUtils.isNoneEmpty(" bob ", null) = false
  10. * StringUtils.isNoneEmpty(" ", "bar") = true
  11. * StringUtils.isNoneEmpty("foo", "bar") = true
  12. * </pre>
  13. *
  14. * @param css the CharSequences to check, may be null or empty
  15. * @return {@code true} if none of the CharSequences are empty or null
  16. * @since 3.2
  17. */
  18. public static boolean isNoneEmpty(final CharSequence... css) {

isBank系列

StringUtils.isBlank()

是否为真空值(空格或者空值)

  1. StringUtils.isBlank(null) =true
  2. StringUtils.isBlank("") =true
  3. StringUtils.isBlank(" ") =true
  4. StringUtils.isBlank("bob") =false
  5. StringUtils.isBlank("bob ") =false
  1. /**
  2. * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
  3. * @paramcs the CharSequence to check, may be null
  4. * @return{@codetrue} if the CharSequence is null, empty or whitespace
  5. * @since2.0
  6. * @since3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
  7. */
  8. publicstaticbooleanisBlank(finalCharSequence cs){
  9. intstrLen;
  10. if(cs ==null|| (strLen = cs.length()) ==0) {
  11. returntrue;
  12. }
  13. for(inti =0; i < strLen; i++) {
  14. if(Character.isWhitespace(cs.charAt(i)) ==false) {
  15. returnfalse;
  16. }
  17. }
  18. returntrue;
  19. }

StringUtils.isNotBlank()

是否真的不为空,不是空格或者空值 ,相当于!isBlank();

  1. publicstaticbooleanisNotBlank(finalCharSequence cs){
  2. return!isBlank(cs);
  3. }

StringUtils.isAnyBlank()

是否包含任何真空值(包含空格或空值)

  1. StringUtils.isAnyBlank(null) =true
  2. StringUtils.isAnyBlank(null,"foo") =true
  3. StringUtils.isAnyBlank(null,null) =true
  4. StringUtils.isAnyBlank("","bar") =true
  5. StringUtils.isAnyBlank("bob","") =true
  6. StringUtils.isAnyBlank("bob ",null) =true
  7. StringUtils.isAnyBlank(" ","bar") =true
  8. StringUtils.isAnyBlank("foo","bar") =false
  1. /**
  2. * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
  3. * @paramcss the CharSequences to check, may be null or empty
  4. * @return{@codetrue} if any of the CharSequences are blank or null or whitespace only
  5. * @since3.2
  6. */
  7. publicstaticbooleanisAnyBlank(finalCharSequence... css){
  8. if(ArrayUtils.isEmpty(css)) {
  9. returntrue;
  10. }
  11. for(finalCharSequence cs : css){
  12. if(isBlank(cs)) {
  13. returntrue;
  14. }
  15. }
  16. returnfalse;
  17. }

StringUtils.isNoneBlank()

是否全部都不包含空值或空格

  1. StringUtils.isNoneBlank(null) =false
  2. StringUtils.isNoneBlank(null,"foo") =false
  3. StringUtils.isNoneBlank(null,null) =false
  4. StringUtils.isNoneBlank("","bar") =false
  5. StringUtils.isNoneBlank("bob","") =false
  6. StringUtils.isNoneBlank("bob ",null) =false
  7. StringUtils.isNoneBlank(" ","bar") =false
  8. StringUtils.isNoneBlank("foo","bar") =true
  1. /**
  2. * <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
  3. * @paramcss the CharSequences to check, may be null or empty
  4. * @return{@codetrue} if none of the CharSequences are blank or null or whitespace only
  5. * @since3.2
  6. */
  7. publicstaticbooleanisNoneBlank(finalCharSequence... css){
  8. return!isAnyBlank(css);
  9. }

StringUtils的其他方法

可以参考官方的文档,里面有详细的描述,有些方法还是很好用的.
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html