+ * Checks if a String is empty ("") or null. + *
+ * + *+ * StringUtils.isEmpty(null) = true + * StringUtils.isEmpty("") = true + * StringUtils.isEmpty(" ") = false + * StringUtils.isEmpty("bob") = false + * StringUtils.isEmpty(" bob ") = false + *+ * + *
+ * NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank(). + *
+ * + * @param str + * the String to check, may be null + * @returntrue
if the String is empty or null
+ */
+ public static boolean isEmpty(String str) {
+ return str == null || str.length() == 0;
+ }
+
+ /**
+ * + * Checks if a String is whitespace, empty ("") or null. + *
+ * + *+ * StringUtils.isBlank(null) = true + * StringUtils.isBlank("") = true + * StringUtils.isBlank(" ") = true + * StringUtils.isBlank("bob") = false + * StringUtils.isBlank(" bob ") = false + *+ * + * @param str + * the String to check, may be null + * @return
true
if the String is null, empty or whitespace
+ */
+ public static boolean isBlank(String str) {
+ int strLen;
+ if (str == null || (strLen = str.length()) == 0) {
+ return true;
+ }
+ for (int i = 0; i < strLen; i++) {
+ if ((Character.isWhitespace(str.charAt(i)) == false)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ *
+ * Removes control characters (char <= 32) from both ends of this String returning null
if the String is empty
+ * ("") after the trim or if it is null
.
+ *
+ *
+ * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. To strip whitespace use + * {@link #stripToNull(String)}. + *
+ * + *+ * StringUtils.trimToNull(null) = null + * StringUtils.trimToNull("") = null + * StringUtils.trimToNull(" ") = null + * StringUtils.trimToNull("abc") = "abc" + * StringUtils.trimToNull(" abc ") = "abc" + *+ * + * @param str + * the String to be trimmed, may be null + * @return the trimmed String,
null
if only chars <= 32, empty or null String input
+ * @since 2.0
+ */
+ public static String trimToNull(String str) {
+ String ts = trim(str);
+ return isEmpty(ts) ? null : ts;
+ }
+
+ /**
+ *
+ * Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty
+ * ("") after the trim or if it is null
.
+ *
+ *
+ * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. To strip whitespace use + * {@link #stripToEmpty(String)}. + *
+ * + *+ * StringUtils.trimToEmpty(null) = "" + * StringUtils.trimToEmpty("") = "" + * StringUtils.trimToEmpty(" ") = "" + * StringUtils.trimToEmpty("abc") = "abc" + * StringUtils.trimToEmpty(" abc ") = "abc" + *+ * + * @param str + * the String to be trimmed, may be null + * @return the trimmed String, or an empty String if
null
input
+ * @since 2.0
+ */
+ public static String trimToEmpty(String str) {
+ return str == null ? EMPTY : str.trim();
+ }
+
+ /**
+ *
+ * Removes control characters (char <= 32) from both ends of this String, handling null
by returning
+ * null
.
+ *
+ * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. To strip whitespace use + * {@link #strip(String)}. + *
+ * + *+ * To trim your choice of characters, use the {@link #strip(String, String)} methods. + *
+ * + *+ * StringUtils.trim(null) = null + * StringUtils.trim("") = "" + * StringUtils.trim(" ") = "" + * StringUtils.trim("abc") = "abc" + * StringUtils.trim(" abc ") = "abc" + *+ * + * @param str + * the String to be trimmed, may be null + * @return the trimmed string,
null
if null String input
+ */
+ public static String trim(String str) {
+ return str == null ? null : str.trim();
+ }
+
+ /**
+ *
+ * Compares two Strings, returning true
if they are equal.
+ *
+ * null
s are handled without exceptions. Two null
references are considered to be equal. The comparison
+ * is case sensitive.
+ *
+ * StringUtils.equals(null, null) = true + * StringUtils.equals(null, "abc") = false + * StringUtils.equals("abc", null) = false + * StringUtils.equals("abc", "abc") = true + * StringUtils.equals("abc", "ABC") = false + *+ * + * @see java.lang.String#equals(Object) + * @param str1 + * the first String, may be null + * @param str2 + * the second String, may be null + * @return
true
if the Strings are equal, case sensitive, or both null
+ */
+ public static boolean equals(String str1, String str2) {
+ return str1 == null ? str2 == null : str1.equals(str2);
+ }
+
+ /**
+ *
+ * Compares two Strings, returning true
if they are equal ignoring the case.
+ *
+ * null
s are handled without exceptions. Two null
references are considered equal. Comparison is case
+ * insensitive.
+ *
+ * StringUtils.equalsIgnoreCase(null, null) = true + * StringUtils.equalsIgnoreCase(null, "abc") = false + * StringUtils.equalsIgnoreCase("abc", null) = false + * StringUtils.equalsIgnoreCase("abc", "abc") = true + * StringUtils.equalsIgnoreCase("abc", "ABC") = true + *+ * + * @see java.lang.String#equalsIgnoreCase(String) + * @param str1 + * the first String, may be null + * @param str2 + * the second String, may be null + * @return
true
if the Strings are equal, case insensitive, or both null
+ */
+ public static boolean equalsIgnoreCase(String str1, String str2) {
+ return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
+ }
+
+ /**
+ * + * Check if a String starts with a specified prefix. + *
+ * + *
+ * null
s are handled without exceptions. Two null
references are considered to be equal. The comparison
+ * is case sensitive.
+ *
+ * StringUtils.startsWith(null, null) = true + * StringUtils.startsWith(null, "abc") = false + * StringUtils.startsWith("abcdef", null) = false + * StringUtils.startsWith("abcdef", "abc") = true + * StringUtils.startsWith("ABCDEF", "abc") = false + *+ * + * @see java.lang.String#startsWith(String) + * @param str + * the String to check, may be null + * @param prefix + * the prefix to find, may be null + * @return
true
if the String starts with the prefix, case sensitive, or both null
+ * @since 2.4
+ */
+ public static boolean startsWith(String str, String prefix) {
+ return startsWith(str, prefix, false);
+ }
+
+ /**
+ * + * Case insensitive check if a String starts with a specified prefix. + *
+ * + *
+ * null
s are handled without exceptions. Two null
references are considered to be equal. The comparison
+ * is case insensitive.
+ *
+ * StringUtils.startsWithIgnoreCase(null, null) = true + * StringUtils.startsWithIgnoreCase(null, "abc") = false + * StringUtils.startsWithIgnoreCase("abcdef", null) = false + * StringUtils.startsWithIgnoreCase("abcdef", "abc") = true + * StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true + *+ * + * @see java.lang.String#startsWith(String) + * @param str + * the String to check, may be null + * @param prefix + * the prefix to find, may be null + * @return
true
if the String starts with the prefix, case insensitive, or both null
+ * @since 2.4
+ */
+ public static boolean startsWithIgnoreCase(String str, String prefix) {
+ return startsWith(str, prefix, true);
+ }
+
+ /**
+ * + * Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false. + *
+ * + *
+ * null
will return false
. An empty String (length()=0) will return true
.
+ *
+ * StringUtils.isNumeric(null) = false + * StringUtils.isNumeric("") = true + * StringUtils.isNumeric(" ") = false + * StringUtils.isNumeric("123") = true + * StringUtils.isNumeric("12 3") = false + * StringUtils.isNumeric("ab2c") = false + * StringUtils.isNumeric("12-3") = false + * StringUtils.isNumeric("12.3") = false + *+ * + * @param str + * the String to check, may be null + * @return
true
if only contains digits, and is non-null
+ */
+ public static boolean isNumeric(String str) {
+ if (str == null) {
+ return false;
+ }
+ int sz = str.length();
+ for (int i = 0; i < sz; i++) {
+ if (Character.isDigit(str.charAt(i)) == false) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * + * Check if a String starts with a specified prefix (optionally case insensitive). + *
+ * + * @see java.lang.String#startsWith(String) + * @param str + * the String to check, may be null + * @param prefix + * the prefix to find, may be null + * @param ignoreCase + * inidicates whether the compare should ignore case (case insensitive) or not. + * @returntrue
if the String starts with the prefix or both null
+ */
+ private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
+ if (str == null || prefix == null) {
+ return (str == null && prefix == null);
+ }
+ if (prefix.length() > str.length()) {
+ return false;
+ }
+ return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
+ }
+
+ public static interface StringFormatter