String处理方法

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字符串则返回<code>null</code>  * @throws IllegalArgumentException  *             如果长度小于 4  * @author ZhangFei 2008-07-11  */ public static String htmlCutShort(String str, int maxWidth) {  return htmlEncode(cutShort(str, maxWidth)); }  /**  * <p>  * 使用省略号(...)将过长的字符串截短。  *   * <p>  * 使用注意:  * <ul>  * <li>如果 <code>str</code>的长度短于<code>maxWidth</code> 个字符个数,返回<code>str</code>本身.</li>  * <li>如果 <code>str</code>的长度长于<code>maxWidth</code> 个字符个数,将被截短到  * <code>(substring(str, 0, max-3) + "...")</code>.</li>  * <li>如果 <code>maxWidth</code> 小于 <code>4</code>, 异常  * <code>IllegalArgumentException</code>将会抛出.</li>  * <li>返回的字符串的长度永远不会长于 <code>maxWidth</code>.</li>  * </ul>  * </p>  *   * <pre>  *   StringUtil.cutShort(null, *)      = null  *   StringUtil.cutShort(&quot;&quot;, 4)        = &quot;&quot;  *   StringUtil.cutShort(&quot;abcdefg&quot;, 6) = &quot;abc...&quot;  *   StringUtil.cutShort(&quot;abcdefg&quot;, 7) = &quot;abcdefg&quot;  *   StringUtil.cutShort(&quot;abcdefg&quot;, 8) = &quot;abcdefg&quot;  *   StringUtil.cutShort(&quot;abcdefg&quot;, 4) = &quot;a...&quot;  *   StringUtil.cutShort(&quot;abcdefg&quot;, 3) = IllegalArgumentException  * </pre>  *   * @param str  *            要被截短的字符串,可以为 null  * @param maxWidth  *            截短后字符串的最大长度, 但必须大于等于 4  * @return 截短后的字符串, 如果传入null字符串则返回<code>null</code>  * @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标记所用的六个特殊字符:& / " ' &lt; &gt;<br>  *   * @param sSource  *            要替换的字符串。  * @return 返回替换后的字符串。  * @author ZhangFei 2008-07-11  */ public static String htmlEncode(String sSource) {  String sTemp = sSource;   sTemp = replaces(sTemp, "&", "&amp;");  sTemp = replaces(sTemp, "/"", "&quot;");  sTemp = replaces(sTemp, "'", "&#039;");  sTemp = replaces(sTemp, "<", "&lt;");  sTemp = replaces(sTemp, ">", "&gt;");  return sTemp; }  /**  * <p>  * 在字符串中进行查找替换,给出替换后的字符串.  * </p>  *   * <p>  * 如果是 <code>null</code> 传入将返回不替换而返回原字符串.  * </p>  * 例子:  *   * <pre>  *   StringUtil.replaces(null, *, *)        = null  *   StringUtil.replaces(&quot;&quot;, *, *)          = &quot;&quot;  *   StringUtil.replaces(&quot;aba&quot;, null, null) = &quot;aba&quot;  *   StringUtil.replaces(&quot;aba&quot;, null, null) = &quot;aba&quot;  *   StringUtil.replaces(&quot;aba&quot;, &quot;a&quot;, null)  = &quot;aba&quot;  *   StringUtil.replaces(&quot;aba&quot;, &quot;a&quot;, &quot;&quot;)    = &quot;aba&quot;  *   StringUtil.replaces(&quot;aba&quot;, &quot;a&quot;, &quot;z&quot;)   = &quot;zbz&quot;  * </pre>  *   * @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, <code>null</code> 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(); }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值