一、前言
关于hibernate(3.0.1)源码包中org.hibernate.util.StringHelper字符串帮助处理类,对字符串进行查找右边位置lastIndexOfLetter、字符串数组进行按照分割进行连接join、字符串重复次数组装repeat、字符串替换分割等常用处理。
二、源码说明
package org.hibernate.util;@b@@b@import java.util.Iterator;@b@import java.util.StringTokenizer;@b@@b@public final class StringHelper@b@{@b@ private static final int ALIAS_TRUNCATE_LENGTH = 10;@b@@b@ public static int lastIndexOfLetter(String string)@b@ {@b@ for (int i = 0; i < string.length(); ++i) {@b@ char character = string.charAt(i);@b@ if (!(Character.isLetter(character))) return (i - 1);@b@ }@b@ return (string.length() - 1);@b@ }@b@@b@ public static String join(String seperator, String[] strings) {@b@ int length = strings.length;@b@ if (length == 0) return "";@b@ StringBuffer buf = new StringBuffer(length * strings[0].length()).append(strings[0]);@b@@b@ for (int i = 1; i < length; ++i)@b@ buf.append(seperator).append(strings[i]);@b@@b@ return buf.toString();@b@ }@b@@b@ public static String join(String seperator, Iterator objects) {@b@ StringBuffer buf = new StringBuffer();@b@ if (objects.hasNext()) buf.append(objects.next());@b@ while (objects.hasNext())@b@ buf.append(seperator).append(objects.next());@b@@b@ return buf.toString();@b@ }@b@@b@ public static String[] add(String[] x, String sep, String[] y) {@b@ String[] result = new String[x.length];@b@ for (int i = 0; i < x.length; ++i)@b@ result[i] = x[i] + sep + y[i];@b@@b@ return result;@b@ }@b@@b@ public static String repeat(String string, int times) {@b@ StringBuffer buf = new StringBuffer(string.length() * times);@b@ for (int i = 0; i < times; ) { buf.append(string); ++i; }@b@ return buf.toString();@b@ }@b@@b@ public static String replace(String template, String placeholder, String replacement)@b@ {@b@ return replace(template, placeholder, replacement, false);@b@ }@b@@b@ public static String[] replace(String[] templates, String placeholder, String replacement) {@b@ String[] result = new String[templates.length];@b@ for (int i = 0; i < templates.length; ++i)@b@ result[i] = replace(templates[i], placeholder, replacement);@b@@b@ return result;@b@ }@b@@b@ public static String replace(String template, String placeholder, String replacement, boolean wholeWords) {@b@ int loc = template.indexOf(placeholder);@b@ if (loc < 0) {@b@ return template;@b@ }@b@@b@ boolean actuallyReplace = (!(wholeWords)) || (loc + placeholder.length() == template.length()) || (!(Character.isJavaIdentifierPart(template.charAt(loc + placeholder.length()))));@b@@b@ String actualReplacement = (actuallyReplace) ? replacement : placeholder;@b@ return template.substring(0, loc) + actualReplacement + replace(template.substring(loc + placeholder.length()), placeholder, replacement, wholeWords);@b@ }@b@@b@ public static String replaceOnce(String template, String placeholder, String replacement)@b@ {@b@ int loc = template.indexOf(placeholder);@b@ if (loc < 0) {@b@ return template;@b@ }@b@@b@ return template.substring(0, loc) + replacement + template.substring(loc + placeholder.length());@b@ }@b@@b@ public static String[] split(String seperators, String list)@b@ {@b@ return split(seperators, list, false);@b@ }@b@@b@ public static String[] split(String seperators, String list, boolean include) {@b@ StringTokenizer tokens = new StringTokenizer(list, seperators, include);@b@ String[] result = new String[tokens.countTokens()];@b@ int i = 0;@b@ while (tokens.hasMoreTokens())@b@ result[(i++)] = tokens.nextToken();@b@@b@ return result;@b@ }@b@@b@ public static String unqualify(String qualifiedName) {@b@ return qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1);@b@ }@b@@b@ public static String qualifier(String qualifiedName) {@b@ int loc = qualifiedName.lastIndexOf(".");@b@ return ((loc < 0) ? "" : qualifiedName.substring(0, loc));@b@ }@b@@b@ public static String[] suffix(String[] columns, String suffix) {@b@ if (suffix == null) return columns;@b@ String[] qualified = new String[columns.length];@b@ for (int i = 0; i < columns.length; ++i)@b@ qualified[i] = suffix(columns[i], suffix);@b@@b@ return qualified;@b@ }@b@@b@ private static String suffix(String name, String suffix) {@b@ return name + suffix;@b@ }@b@@b@ public static String root(String qualifiedName) {@b@ int loc = qualifiedName.indexOf(".");@b@ return ((loc < 0) ? qualifiedName : qualifiedName.substring(0, loc));@b@ }@b@@b@ public static String unroot(String qualifiedName) {@b@ int loc = qualifiedName.indexOf(".");@b@ return ((loc < 0) ? qualifiedName : qualifiedName.substring(loc + 1, qualifiedName.length()));@b@ }@b@@b@ public static boolean booleanValue(String tfString) {@b@ String trimmed = tfString.trim().toLowerCase();@b@ return ((trimmed.equals("true")) || (trimmed.equals("t")));@b@ }@b@@b@ public static String toString(Object[] array) {@b@ int len = array.length;@b@ if (len == 0) return "";@b@ StringBuffer buf = new StringBuffer(len * 12);@b@ for (int i = 0; i < len - 1; ++i)@b@ buf.append(array[i]).append(", ");@b@@b@ return array[(len - 1)];@b@ }@b@@b@ public static String[] multiply(String string, Iterator placeholders, Iterator replacements) {@b@ String[] result = { string };@b@ while (placeholders.hasNext())@b@ result = multiply(result, (String)placeholders.next(), (String[])replacements.next());@b@@b@ return result;@b@ }@b@@b@ private static String[] multiply(String[] strings, String placeholder, String[] replacements) {@b@ String[] results = new String[replacements.length * strings.length];@b@ int n = 0;@b@ for (int i = 0; i < replacements.length; ++i)@b@ for (int j = 0; j < strings.length; ++j)@b@ results[(n++)] = replaceOnce(strings[j], placeholder, replacements[i]);@b@@b@@b@ return results;@b@ }@b@@b@ public static int countUnquoted(String string, char character) {@b@ if ('\'' == character)@b@ throw new IllegalArgumentException("Unquoted count of quotes is invalid");@b@@b@ if (string == null) {@b@ return 0;@b@ }@b@@b@ int count = 0;@b@ int stringLength = string.length();@b@ boolean inQuote = false;@b@ for (int indx = 0; indx < stringLength; ++indx) {@b@ char c = string.charAt(indx);@b@ if (inQuote) {@b@ if ('\'' == c)@b@ inQuote = false;@b@@b@ }@b@ else if ('\'' == c) {@b@ inQuote = true;@b@ }@b@ else if (c == character)@b@ ++count;@b@ }@b@@b@ return count;@b@ }@b@@b@ public static boolean isNotEmpty(String string) {@b@ return ((string != null) && (string.length() > 0));@b@ }@b@@b@ public static boolean isEmpty(String string) {@b@ return ((string == null) || (string.length() == 0));@b@ }@b@@b@ public static String qualify(String prefix, String name) {@b@ if ((name == null) || (prefix == null))@b@ throw new NullPointerException();@b@@b@ return (prefix.length() + name.length() + 1) + prefix + '.' + name;@b@ }@b@@b@ public static String[] qualify(String prefix, String[] names)@b@ {@b@ if (prefix == null) return names;@b@ int len = names.length;@b@ String[] qualified = new String[len];@b@ for (int i = 0; i < len; ++i)@b@ qualified[i] = qualify(prefix, names[i]);@b@@b@ return qualified;@b@ }@b@@b@ public static int firstIndexOfChar(String sqlString, String string, int startindex) {@b@ int matchAt = -1;@b@ for (int i = 0; i < string.length(); ++i) {@b@ int curMatch = sqlString.indexOf(string.charAt(i), startindex);@b@ if (curMatch >= 0)@b@ if (matchAt == -1) {@b@ matchAt = curMatch;@b@ }@b@ else@b@ matchAt = Math.min(matchAt, curMatch);@b@@b@ }@b@@b@ return matchAt;@b@ }@b@@b@ public static String truncate(String string, int length) {@b@ if (string.length() <= length) {@b@ return string;@b@ }@b@@b@ return string.substring(0, length);@b@ }@b@@b@ public static String generateAlias(String description, int unique)@b@ {@b@ return generateAliasRoot(description) + Integer.toString(unique) + '_';@b@ }@b@@b@ private static String generateAliasRoot(String description)@b@ {@b@ String result = truncate(unqualifyEntityName(description), 10).toLowerCase().replace('/', '_').replace('$', '_');@b@@b@ if (Character.isDigit(result.charAt(result.length() - 1))) {@b@ return result + "x";@b@ }@b@@b@ return result;@b@ }@b@@b@ public static String unqualifyEntityName(String entityName)@b@ {@b@ String result = unqualify(entityName);@b@ int slashPos = result.indexOf(47);@b@ if (slashPos > 0)@b@ result = result.substring(0, slashPos - 1);@b@@b@ return result;@b@ }@b@@b@ public static String generateAlias(String description) {@b@ return generateAliasRoot(description) + '_';@b@ }@b@@b@ public static String toUpperCase(String str) {@b@ return ((str == null) ? null : str.toUpperCase());@b@ }@b@@b@ public static String toLowerCase(String str) {@b@ return ((str == null) ? null : str.toLowerCase());@b@ }@b@@b@ public static String moveAndToBeginning(String filter) {@b@ if (filter.trim().length() > 0) {@b@ filter = filter + " and ";@b@ if (filter.startsWith(" and ")) filter = filter.substring(4);@b@ }@b@ return filter;@b@ }@b@}