package common; import java.security.MessageDigest;import java.util.List; /** * 常见的字符串处理方法集合类 */public class StringTools{ /** * 取得指定字符串的md5值 * @param piStr * @return */ public static String md5(String piStr) { if(StringTools.isNullOrSpace(piStr)) piStr=""; String encodeStr = ""; byte[] digesta = null; try { MessageDigest alg = MessageDigest.getInstance("MD5"); alg.update(piStr.getBytes()); digesta = alg.digest(); encodeStr = byte2hex(digesta); } catch (Exception e) { } return encodeStr; } private static String byte2hex(byte[] piByte) { String reStr = ""; String tmpStr = ""; for (int i = 0; i < piByte.length; i++) { int v = piByte[i] & 0xFF; if (v < 16) reStr += "0"; reStr += Integer.toString(v, 16).toLowerCase(); } return reStr; } /** * 转换一个ISO字符串到GB2312 * @param str * @return */ public static String iso2GB(String str) { try { if (str != null) { str = new String(str.getBytes("ISO-8859-1"), "GB2312"); } } catch (Exception e) { e.printStackTrace(); } return str; } /** * 转换一个GB2312字符串到ISO * @param str * @return */ public static String gb2ISO(String str) { try { if (str != null) { str = new String(str.getBytes("GB2312"), "ISO-8859-1"); } } catch (Exception e) { e.printStackTrace(); } return str; } /** * 判断一个字符串是否为NULL或空 * @param str * @return */ public static boolean isNullOrSpace(String str) { if(str==null || str.trim().length()<1) return true; else return false; } /** * 使用给定的字串替换源字符串中指定的字串。 * @param mainString 源字符串 * @param oldString 被替换的字串 * @param newString 替换字串 * @return String */ public final static String replace(final String mainString, final String oldString, final String newString) { if(mainString == null) return null; int i = mainString.lastIndexOf(oldString); if(i < 0) return mainString; StringBuffer mainSb = new StringBuffer(mainString); while (i >= 0) { mainSb.replace(i, i + oldString.length(), newString); i = mainString.lastIndexOf(oldString, i - 1); } return mainSb.toString(); } /** * 去掉字符串的'和“ * @param s * @return */ public final static String safeStr(String s) { String ret = replace(s, "'", ""); ret = replace(ret, "/"", ""); return ret; } public final static String getResultCols(List result, String strSplit) { StringBuffer ret = new StringBuffer(); for(int i = 0; i < result.size(); i++) { ret.append(result.get(i).toString()); ret.append(strSplit); } if(ret != null && ret.length() > 0) return ret.substring(0, ret.length() - strSplit.length()); else return ret.toString(); } /** * 使用省略号(...)将过长的字符串截短并转码html中的特殊字符 * * @param str * 要被截短的字符串,可以为 null * @param maxWidth * 截短后字符串的最大长度, 但必须大于等于 4 * @return 截短后并转码了html中的特殊字符的字符串, 如果传入null字符串则返回
null
* @throws IllegalArgumentException * 如果长度小于 4 * @author ZhangFei 2008-07-11 */ public static String htmlCutShort(String str, int maxWidth) { return htmlEncode(cutShort(str, maxWidth)); } /** *
* 使用省略号(...)将过长的字符串截短。 * *
* 使用注意: *
* - 如果
str
的长度短于maxWidth
个字符个数,返回str
本身. * - 如果
str
的长度长于maxWidth
个字符个数,将被截短到 * (substring(str, 0, max-3) + "...")
. * - 如果
maxWidth
小于 4
, 异常 * IllegalArgumentException
将会抛出. * - 返回的字符串的长度永远不会长于
maxWidth
. *
* * *
* StringUtil.cutShort(null, *) = null * StringUtil.cutShort("", 4) = "" * StringUtil.cutShort("abcdefg", 6) = "abc..." * StringUtil.cutShort("abcdefg", 7) = "abcdefg" * StringUtil.cutShort("abcdefg", 8) = "abcdefg" * StringUtil.cutShort("abcdefg", 4) = "a..." * StringUtil.cutShort("abcdefg", 3) = IllegalArgumentException *
* * @param str * 要被截短的字符串,可以为 null * @param maxWidth * 截短后字符串的最大长度, 但必须大于等于 4 * @return 截短后的字符串, 如果传入null字符串则返回
null
* @throws IllegalArgumentException * 如果长度小于 4 * @author ZhangFei 2008-07-11 */ public static String cutShort(String str, int maxWidth) { if (str == null) return null; String sTemp = str.trim(); if (sTemp.equals("")) return ""; if (sTemp.length() <= maxWidth) return str; if (sTemp.length() > maxWidth) { sTemp = sTemp.substring(0, maxWidth - 3) + "..."; return sTemp; } return ""; } /** * 在字符串中替换html标记所用的六个特殊字符:& / " ' < >
* * @param sSource * 要替换的字符串。 * @return 返回替换后的字符串。 * @author ZhangFei 2008-07-11 */ public static String htmlEncode(String sSource) { String sTemp = sSource; sTemp = replaces(sTemp, "&", "&"); sTemp = replaces(sTemp, "/"", """); sTemp = replaces(sTemp, "'", "'"); sTemp = replaces(sTemp, "<", "<"); sTemp = replaces(sTemp, ">", ">"); return sTemp; } /** *
* 在字符串中进行查找替换,给出替换后的字符串. *
* *
* 如果是 null
传入将返回不替换而返回原字符串. *
* 例子: * *
* StringUtil.replaces(null, *, *) = null * StringUtil.replaces("", *, *) = "" * StringUtil.replaces("aba", null, null) = "aba" * StringUtil.replaces("aba", null, null) = "aba" * StringUtil.replaces("aba", "a", null) = "aba" * StringUtil.replaces("aba", "a", "") = "aba" * StringUtil.replaces("aba", "a", "z") = "zbz" *
* * @param text * text to search and replace in, may be null * @param repl * the String to search for, may be null * @param with * the String to replace with, may be null * @return the text with any replacements processed,
null
if * null String input * @author ZhangFei 2008-07-11 */ public static String replaces(String text, String repl, String with) { return StringTools.replace(text, repl, with); } /** * use specific string to replace specific char * @author ZhangFei 2008-07-11 */ public static String replace(String str, char src, String dst) { int strLen = str.length(); int size = strLen + (str.length() / 20) * dst.length() + 20; StringBuffer sb = new StringBuffer(size); int offset = 0; int index = 0; while (offset < strLen && (index = str.indexOf((int) src, offset)) != -1) { sb.append(str.substring(offset, index)); sb.append(dst); offset = ++index; } if (offset < strLen) sb.append(str.substring(offset)); return sb.toString(); }}