|
| 1 | +package me.chanjar.weixin.common.util; |
| 2 | + |
| 3 | +/** |
| 4 | + * copy from apache-commons-lang3 |
| 5 | + */ |
| 6 | +public class StringUtils { |
| 7 | + |
| 8 | + /** |
| 9 | + * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p> |
| 10 | + * |
| 11 | + * <pre> |
| 12 | + * StringUtils.isBlank(null) = true |
| 13 | + * StringUtils.isBlank("") = true |
| 14 | + * StringUtils.isBlank(" ") = true |
| 15 | + * StringUtils.isBlank("bob") = false |
| 16 | + * StringUtils.isBlank(" bob ") = false |
| 17 | + * </pre> |
| 18 | + * |
| 19 | + * @param cs the CharSequence to check, may be null |
| 20 | + * @return {@code true} if the CharSequence is null, empty or whitespace |
| 21 | + * @since 2.0 |
| 22 | + * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) |
| 23 | + */ |
| 24 | + public static boolean isBlank(CharSequence cs) { |
| 25 | + int strLen; |
| 26 | + if (cs == null || (strLen = cs.length()) == 0) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + for (int i = 0; i < strLen; i++) { |
| 30 | + if (Character.isWhitespace(cs.charAt(i)) == false) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> |
| 39 | + * |
| 40 | + * <pre> |
| 41 | + * StringUtils.isNotBlank(null) = false |
| 42 | + * StringUtils.isNotBlank("") = false |
| 43 | + * StringUtils.isNotBlank(" ") = false |
| 44 | + * StringUtils.isNotBlank("bob") = true |
| 45 | + * StringUtils.isNotBlank(" bob ") = true |
| 46 | + * </pre> |
| 47 | + * |
| 48 | + * @param cs the CharSequence to check, may be null |
| 49 | + * @return {@code true} if the CharSequence is |
| 50 | + * not empty and not null and not whitespace |
| 51 | + * @since 2.0 |
| 52 | + * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence) |
| 53 | + */ |
| 54 | + public static boolean isNotBlank(CharSequence cs) { |
| 55 | + return !StringUtils.isBlank(cs); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * <p>Checks if a CharSequence is empty ("") or null.</p> |
| 60 | + * |
| 61 | + * <pre> |
| 62 | + * StringUtils.isEmpty(null) = true |
| 63 | + * StringUtils.isEmpty("") = true |
| 64 | + * StringUtils.isEmpty(" ") = false |
| 65 | + * StringUtils.isEmpty("bob") = false |
| 66 | + * StringUtils.isEmpty(" bob ") = false |
| 67 | + * </pre> |
| 68 | + * |
| 69 | + * <p>NOTE: This method changed in Lang version 2.0. |
| 70 | + * It no longer trims the CharSequence. |
| 71 | + * That functionality is available in isBlank().</p> |
| 72 | + * |
| 73 | + * @param cs the CharSequence to check, may be null |
| 74 | + * @return {@code true} if the CharSequence is empty or null |
| 75 | + * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) |
| 76 | + */ |
| 77 | + public static boolean isEmpty(CharSequence cs) { |
| 78 | + return cs == null || cs.length() == 0; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * <p>Checks if a CharSequence is not empty ("") and not null.</p> |
| 83 | + * |
| 84 | + * <pre> |
| 85 | + * StringUtils.isNotEmpty(null) = false |
| 86 | + * StringUtils.isNotEmpty("") = false |
| 87 | + * StringUtils.isNotEmpty(" ") = true |
| 88 | + * StringUtils.isNotEmpty("bob") = true |
| 89 | + * StringUtils.isNotEmpty(" bob ") = true |
| 90 | + * </pre> |
| 91 | + * |
| 92 | + * @param cs the CharSequence to check, may be null |
| 93 | + * @return {@code true} if the CharSequence is not empty and not null |
| 94 | + * @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence) |
| 95 | + */ |
| 96 | + public static boolean isNotEmpty(CharSequence cs) { |
| 97 | + return !StringUtils.isEmpty(cs); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments