一、前言
关于empire-db源码包中org.apache.empire.commons.StringUtils字符串工具类,对对象及对象数组字符串间转换toString、对象数组分隔符字符串合并arrayToString、空判断isEmpty、空校验isValid及大小写判断isUpper等。
二、源码说明
package org.apache.empire.commons;@b@@b@public class StringUtils@b@{@b@ public static String toString(Object value, String defValue)@b@ {@b@ return ((value != null) ? value.toString() : defValue);@b@ }@b@@b@ public static String toString(Object value)@b@ {@b@ return toString(value, null);@b@ }@b@@b@ public static String toString(Object[] array, String defValue)@b@ {@b@ String s = arrayToString(array, "/");@b@ return ((s != null) ? s : defValue);@b@ }@b@@b@ public static String toString(Object[] array)@b@ {@b@ return toString(array, null);@b@ }@b@@b@ public static String valueOf(Object value)@b@ {@b@ return toString(value, "null");@b@ }@b@@b@ public static String valueOf(Object[] array)@b@ {@b@ return toString(array, "null");@b@ }@b@@b@ public static String coalesce(String preferred, String alternative)@b@ {@b@ return ((isValid(preferred)) ? preferred : alternative);@b@ }@b@@b@ public static String nullIfEmpty(Object value)@b@ {@b@ if (value == null)@b@ return null;@b@ String strval = value.toString();@b@ return ((strval.length() == 0) ? null : strval);@b@ }@b@@b@ public static String arrayToString(Object[] array, String separator)@b@ {@b@ if ((array == null) || (array.length < 1))@b@ return null;@b@ if (array.length > 1)@b@ {@b@ StringBuilder buf = new StringBuilder();@b@ for (int i = 0; i < array.length; ++i)@b@ {@b@ if ((i > 0) && (separator != null))@b@ buf.append(separator);@b@ buf.append(array[i]);@b@ }@b@ return buf.toString();@b@ }@b@@b@ return String.valueOf(array[0]);@b@ }@b@@b@ public static boolean isEmpty(String s)@b@ {@b@ return ((s == null) || (s.trim().length() == 0));@b@ }@b@@b@ @Deprecated@b@ public static boolean isValid(String s)@b@ {@b@ return ((s != null) && (s.trim().length() > 0));@b@ }@b@@b@ public static boolean isNotEmpty(String s)@b@ {@b@ return ((s != null) && (s.trim().length() > 0));@b@ }@b@@b@ public static boolean compareEqual(String s1, String s2, boolean ignoreCase)@b@ {@b@ if ((s1 == null) || (s2 == null))@b@ return (s1 == s2);@b@@b@ return ((ignoreCase) ? s1.equalsIgnoreCase(s2) : s1.equals(s2));@b@ }@b@@b@ public static String validate(String s)@b@ {@b@ if (s == null)@b@ return null;@b@ s = s.trim();@b@ if (s.length() == 0)@b@ return null;@b@ return s;@b@ }@b@@b@ public static String replace(String source, String find, String replace)@b@ {@b@ if ((source == null) || (find == null) || (find.length() == 0))@b@ return source;@b@@b@ int index = source.indexOf(find);@b@ if (index < 0)@b@ return source;@b@ if (replace == null)@b@ replace = "";@b@@b@ int len = find.length();@b@ return source.substring(0, index) + replace + replace(source.substring(index + len), find, replace);@b@ }@b@@b@ public static String replaceAll(String s, String find, String replace)@b@ {@b@ if (s == null)@b@ return null;@b@ if (replace == null)@b@ replace = "";@b@ if ((find == null) || ("".equals(find)) || (find.equals(replace)))@b@ {@b@ return s;@b@ }@b@ int start = s.indexOf(find);@b@ if (start < 0)@b@ {@b@ return s;@b@ }@b@@b@ StringBuilder b = new StringBuilder(s.length());@b@ char[] origChars = s.toCharArray();@b@ int findLength = find.length();@b@ int copyFrom = 0;@b@ while (start >= 0)@b@ {@b@ b.append(origChars, copyFrom, start - copyFrom);@b@ if (replace.length() > 0)@b@ b.append(replace);@b@ copyFrom = start + findLength;@b@ start = s.indexOf(find, copyFrom);@b@ }@b@@b@ if (origChars.length > copyFrom)@b@ b.append(origChars, copyFrom, origChars.length - copyFrom);@b@@b@ return b.toString();@b@ }@b@@b@ public static String remove(String s, String remove)@b@ {@b@ return replaceAll(s, remove, null);@b@ }@b@@b@ public static String remove(String s, char c)@b@ {@b@ return replaceAll(s, String.valueOf(c), null);@b@ }@b@@b@ public static String removeBlanks(String s)@b@ {@b@ return remove(s, " ");@b@ }@b@@b@ public static boolean isCharBetween(char c, char beg, char end)@b@ {@b@ return ((c >= beg) && (c <= end));@b@ }@b@@b@ public static boolean isNumber(char c)@b@ {@b@ return ((c >= '0') && (c <= '9'));@b@ }@b@@b@ public static boolean isNumber(String s)@b@ {@b@ if (isEmpty(s))@b@ return false;@b@@b@ for (int i = 0; i < s.length(); ++i)@b@ if (!(isNumber(s.charAt(i))))@b@ return false;@b@ return true;@b@ }@b@@b@ public static boolean isNumeric(char c, boolean decimal)@b@ {@b@ if ((c == '+') || (c == '-'))@b@ return true;@b@@b@ if ((decimal) && (((c == '.') || (c == ','))))@b@ return true;@b@@b@ return isNumber(c);@b@ }@b@@b@ public static boolean isNumeric(String s, boolean decimal)@b@ {@b@ if (isEmpty(s))@b@ return false;@b@@b@ for (int i = 0; i < s.length(); ++i)@b@ {@b@ char c = s.charAt(i);@b@ if ((((c == '+') || (c == '-'))) && (i > 0))@b@ return false;@b@ if (!(isNumeric(c, decimal)))@b@ return false;@b@ }@b@ return true;@b@ }@b@@b@ public static boolean isUpper(char c)@b@ {@b@ return (((c >= 'A') && (c <= 'Z')) || ((c >= 192) && (c <= 221)));@b@ }@b@@b@ public static boolean isUpper(String s, int count)@b@ {@b@ if (isEmpty(s))@b@ return false;@b@ if (count > s.length())@b@ count = s.length();@b@ for (int i = 0; i < count; ++i)@b@ if (isLower(s.charAt(i)))@b@ return false;@b@ return true;@b@ }@b@@b@ public static boolean isLower(char c)@b@ {@b@ return (((c >= 'a') && (c <= 'z')) || ((c >= 223) && (c <= 255)));@b@ }@b@@b@ public static boolean isLower(String s, int count)@b@ {@b@ if (isEmpty(s))@b@ return false;@b@ if (count > s.length())@b@ count = s.length();@b@ for (int i = 0; i < count; ++i)@b@ if (isUpper(s.charAt(i)))@b@ return false;@b@ return true;@b@ }@b@@b@ public static String toUpper(String s, int count)@b@ {@b@ if (isEmpty(s))@b@ return s;@b@ if (s.length() <= count)@b@ return s.toUpperCase();@b@@b@ String start = s.substring(0, count);@b@ return start.toUpperCase() + s.substring(count);@b@ }@b@@b@ public static String toLower(String s, int count)@b@ {@b@ if (isEmpty(s))@b@ return s;@b@ if (s.length() <= count)@b@ return s.toLowerCase();@b@@b@ String start = s.substring(0, count);@b@ return start.toLowerCase() + s.substring(count);@b@ }@b@}