一、前言
基于apache的derby源码包中的org.apache.derby.iapi.util.StringUtil字符串工具类,对字符串进行分隔、格式化、转换及规则处理等操作,具体见如下源码说明。
二、源码说明
package org.apache.derby.iapi.util;@b@@b@import java.util.Locale;@b@import java.util.StringTokenizer;@b@@b@public class StringUtil@b@{@b@ private static char[] hex_table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };@b@@b@ public static String[] split(String paramString, char paramChar)@b@ {@b@ if (paramString == null)@b@ throw new NullPointerException("str can't be null");@b@ StringTokenizer localStringTokenizer = new StringTokenizer(paramString, String.valueOf(paramChar));@b@ int i = localStringTokenizer.countTokens();@b@ String[] arrayOfString = new String[i];@b@ for (int j = 0; j < i; ++j)@b@ arrayOfString[j] = localStringTokenizer.nextToken();@b@ return arrayOfString;@b@ }@b@@b@ public static final String formatForPrint(String paramString)@b@ {@b@ if (paramString.length() > 60)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer(paramString.substring(0, 60));@b@ localStringBuffer.append("&");@b@ paramString = localStringBuffer.toString();@b@ }@b@ return paramString;@b@ }@b@@b@ public static String[] toStringArray(Object[] paramArrayOfObject)@b@ {@b@ int j = paramArrayOfObject.length;@b@ String[] arrayOfString = new String[j];@b@ for (int i = 0; i < j; ++i)@b@ arrayOfString[i] = paramArrayOfObject[i].toString();@b@ return arrayOfString;@b@ }@b@@b@ public static byte[] getAsciiBytes(String paramString)@b@ {@b@ char[] arrayOfChar = paramString.toCharArray();@b@ byte[] arrayOfByte = new byte[arrayOfChar.length];@b@ for (int i = 0; i < arrayOfChar.length; ++i)@b@ arrayOfByte[i] = (byte)(arrayOfChar[i] & 0x7F);@b@ return arrayOfByte;@b@ }@b@@b@ public static String trimTrailing(String paramString)@b@ {@b@ if (paramString == null)@b@ return null;@b@ for (int i = paramString.length(); i > 0; --i)@b@ if (!(Character.isWhitespace(paramString.charAt(i - 1))))@b@ break;@b@ return paramString.substring(0, i);@b@ }@b@@b@ public static String truncate(String paramString, int paramInt)@b@ {@b@ if ((paramString != null) && (paramString.length() > paramInt))@b@ paramString = paramString.substring(0, paramInt);@b@ return paramString;@b@ }@b@@b@ public static String slice(String paramString, int paramInt1, int paramInt2, boolean paramBoolean)@b@ {@b@ String str = paramString.substring(paramInt1, paramInt2 + 1);@b@ if (paramBoolean)@b@ str = str.trim();@b@ return str;@b@ }@b@@b@ public static String toHexString(byte[] paramArrayOfByte, int paramInt1, int paramInt2)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer(paramInt2 * 2);@b@ int i = paramInt1 + paramInt2;@b@ for (int j = paramInt1; j < i; ++j)@b@ {@b@ int k = (paramArrayOfByte[j] & 0xF0) >>> 4;@b@ int l = paramArrayOfByte[j] & 0xF;@b@ localStringBuffer.append(hex_table[k]);@b@ localStringBuffer.append(hex_table[l]);@b@ }@b@ return localStringBuffer.toString();@b@ }@b@@b@ public static byte[] fromHexString(String paramString, int paramInt1, int paramInt2)@b@ {@b@ if (paramInt2 % 2 != 0)@b@ return null;@b@ byte[] arrayOfByte = new byte[paramInt2 / 2];@b@ int i = 0;@b@ int j = paramInt1 + paramInt2;@b@ for (int k = paramInt1; k < j; k += 2)@b@ {@b@ int l = Character.digit(paramString.charAt(k), 16);@b@ int i1 = Character.digit(paramString.charAt(k + 1), 16);@b@ if ((l == -1) || (i1 == -1))@b@ return null;@b@ arrayOfByte[(i++)] = (byte)(l << 4 & 0xF0 | i1 & 0xF);@b@ }@b@ return arrayOfByte;@b@ }@b@@b@ public static String hexDump(byte[] paramArrayOfByte)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer(paramArrayOfByte.length * 3);@b@ localStringBuffer.append("Hex dump:\n");@b@ for (int j = 0; j < paramArrayOfByte.length; j += 16)@b@ {@b@ int l;@b@ String str = Integer.toHexString(j);@b@ for (int k = str.length(); k < 8; ++k)@b@ localStringBuffer.append("0");@b@ localStringBuffer.append(str);@b@ localStringBuffer.append(":");@b@ for (k = 0; (k < 16) && (j + k < paramArrayOfByte.length); ++k)@b@ {@b@ int i = paramArrayOfByte[(j + k)];@b@ if (k % 2 == 0)@b@ localStringBuffer.append(" ");@b@ l = (byte)((i & 0xF0) >>> 4);@b@ int i1 = (byte)(i & 0xF);@b@ localStringBuffer.append(hex_table[l]);@b@ localStringBuffer.append(hex_table[i1]);@b@ }@b@ localStringBuffer.append(" ");@b@ for (k = 0; (k < 16) && (j + k < paramArrayOfByte.length); ++k)@b@ {@b@ l = (char)paramArrayOfByte[(j + k)];@b@ if (Character.isLetterOrDigit(l))@b@ localStringBuffer.append(String.valueOf(l));@b@ else@b@ localStringBuffer.append(".");@b@ }@b@ localStringBuffer.append("\n");@b@ }@b@ return localStringBuffer.toString();@b@ }@b@@b@ public static String SQLToUpperCase(String paramString)@b@ {@b@ return paramString.toUpperCase(Locale.ENGLISH);@b@ }@b@@b@ public static boolean SQLEqualsIgnoreCase(String paramString1, String paramString2)@b@ {@b@ if (paramString2 == null)@b@ return false;@b@ return SQLToUpperCase(paramString1).equals(SQLToUpperCase(paramString2));@b@ }@b@@b@ public static String normalizeSQLIdentifier(String paramString)@b@ {@b@ if (paramString.length() == 0)@b@ return paramString;@b@ if ((paramString.charAt(0) == '"') && (paramString.length() >= 3) && (paramString.charAt(paramString.length() - 1) == '"'))@b@ return compressQuotes(paramString.substring(1, paramString.length() - 1), "\"\"");@b@ return SQLToUpperCase(paramString);@b@ }@b@@b@ public static String compressQuotes(String paramString1, String paramString2)@b@ {@b@ String str = paramString1;@b@ for (int i = str.indexOf(paramString2); i != -1; i = str.indexOf(paramString2, i + 1))@b@ str = str.substring(0, i + 1) + str.substring(i + 2);@b@ return str;@b@ }@b@@b@ static String quoteString(String paramString, char paramChar)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer(paramString.length() + 2);@b@ localStringBuffer.append(paramChar);@b@ for (int i = 0; i < paramString.length(); ++i)@b@ {@b@ char c = paramString.charAt(i);@b@ if (c == paramChar)@b@ localStringBuffer.append(paramChar);@b@ localStringBuffer.append(c);@b@ }@b@ localStringBuffer.append(paramChar);@b@ return localStringBuffer.toString();@b@ }@b@@b@ public static String quoteStringLiteral(String paramString)@b@ {@b@ return quoteString(paramString, '\'');@b@ }@b@@b@ public static String stringify(int[] paramArrayOfInt)@b@ {@b@ if (paramArrayOfInt == null)@b@ return "null";@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ int i = paramArrayOfInt.length;@b@ localStringBuffer.append("[ ");@b@ for (int j = 0; j < i; ++j)@b@ {@b@ if (j > 0)@b@ localStringBuffer.append(", ");@b@ localStringBuffer.append(paramArrayOfInt[j]);@b@ }@b@ localStringBuffer.append(" ]");@b@ return localStringBuffer.toString();@b@ }@b@@b@ public static String ensureIndent(String paramString, int paramInt)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ while (paramInt-- > 0)@b@ localStringBuffer.append("\t");@b@ if (paramString == null)@b@ return localStringBuffer.toString() + "null";@b@ paramString = doRegExpA(paramString, localStringBuffer.toString());@b@ paramString = doRegExpB(paramString);@b@ paramString = doRegExpC(paramString, localStringBuffer.toString());@b@ return paramString;@b@ }@b@@b@ private static String doRegExpA(String paramString1, String paramString2)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ for (int i = 0; (i < paramString1.length()) && (paramString1.charAt(i) == '\t'); ++i);@b@ localStringBuffer.append(paramString2);@b@ localStringBuffer.append(paramString1.substring(i));@b@ return localStringBuffer.toString();@b@ }@b@@b@ private static String doRegExpB(String paramString)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ for (int i = paramString.length() - 1; (i >= 0) && (paramString.charAt(i) == '\n'); --i);@b@ localStringBuffer.append(paramString.substring(0, i + 1));@b@ return localStringBuffer.toString();@b@ }@b@@b@ private static String doRegExpC(String paramString1, String paramString2)@b@ {@b@ StringBuffer localStringBuffer = new StringBuffer();@b@ int i = 0;@b@ while (i < paramString1.length())@b@ {@b@ char c = paramString1.charAt(i);@b@ if (c == '\n')@b@ {@b@ localStringBuffer.append(c);@b@ for (int j = i + 1; (j < paramString1.length()) && (paramString1.charAt(j) == '\t'); ++j);@b@ localStringBuffer.append(paramString2);@b@ i = j;@b@ }@b@ else@b@ {@b@ localStringBuffer.append(c);@b@ ++i;@b@ }@b@ }@b@ return localStringBuffer.toString();@b@ }@b@}