字符串处理类。


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.validator.GenericValidator;


import sun.io.ByteToCharConverter;
import sun.io.MalformedInputException;
public class StringUtil{

  public StringUtil(){
  }
  /**
   *
   * @param s String
   * @return String
   */
  public static String urlDecode(String s){
    String result = null;
    if(s != null)
      result = URLDecoder.decode(s);
    return result;
  }
  /**
   *
   * @param param String
   * @return String
   */
  public static String processParameter(String param){
    if(param == null)
        return null;
    String result = null;
    try{
        result = new String(param.getBytes("ISO-8859-1"), "UTF-8");
        return result;
    }catch(UnsupportedEncodingException e){
        return param;
    }
  }
  /**
   *
   * @param s String
   * @param oldChar char
   * @return String
   */
  public static String deleteAll(String s, char oldChar){
    if(s == null)
      return null;
    String result = null;
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < s.length(); i++){
      char c = s.charAt(i);
      if(c != oldChar)
        sb.append(c);
    }
    result = sb.toString();
    return result;
  }
  /**
   * compare string
   * @param a String
   * @param b String
   * @return boolean
   */
  public static boolean compare(String a, String b){
    if(a == null)
      a = "";
    if(b == null)
      b = "";
    a = a.trim();
    b = b.trim();
    return a.equals(b);
  }
  /**
   *
   * @param email String
   * @return int
   */
  public static int validateEmail(String email){
      if(email != null)
          email = email.trim();
      if(GenericValidator.isBlankOrNull(email))
          return 0;
      return !GenericValidator.isEmail(email) ? -1 : 1;
  }
  /**
   *
   * @param string String
   * @return String
   */
  public static String convertFromISO(String string){
    if(string == null)
      return string;
    try{
      String temp_p = string;
      byte temp_b[] = temp_p.getBytes("ISO8859-1");
      String temp = new String(temp_b, "UTF-8");
      return temp;
    }catch(UnsupportedEncodingException e){
      System.err.println(e);
    }
    return string;
  }
  /**
   *
   * @param string String
   * @return String
   */
  public static String convertToISO(String string){
    if(string == null)
      return string;
    try{
      String temp_p = string;
      byte temp_b[] = temp_p.getBytes("UTF-8");
      String temp = new String(temp_b, "ISO8859-1");
      return temp;
    }catch(UnsupportedEncodingException e){
      System.err.println(e);
    }
    return string;
  }
  /**
   *
   * @param s String
   * @return String
   */
  public static String convertToUTF8(String s){
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < s.length(); i++){
      char c = s.charAt(i);
      if(c >= 0 && c <= '\377'){
        sb.append(c);
      } else{
        byte b[];
        try{
          b = Character.toString(c).getBytes("utf-8");
        }catch(Exception ex){
          System.out.println(ex);
          b = new byte[0];
        }
        for(int j = 0; j < b.length; j++){
          int k = b[j];
          if(k < 0)
            k += 256;
          sb.append("%" + Integer.toHexString(k).toUpperCase());
        }

      }
    }

    return sb.toString();
  }

  /**
   *
   * @param str String
   * @return String
   */

  public static String getEncodedFileName(String str){
    if(str == null || str.length() <= 0)
      return null;
    int dot = str.lastIndexOf(46);
    String s1;
    String s2;
    if(dot >= 0){
      s1 = str.substring(0, dot);
      s2 = str.substring(dot);
    } else{
      s1 = str;
      s2 = "";
    }
    return toUnicodeString(s1) + s2;
  }
  /**
   *
   * @param pStr String
   * @return String
   */
  public static String toUnicodeString(String pStr){
    String encodedName = "";
    try{
      byte namebytes[] = pStr.getBytes("UTF-8");
      ByteToCharConverter converter = ByteToCharConverter.getConverter("UTF-8");
      char cArray[] = converter.convertAll(namebytes);
      for(int i = 0; i < cArray.length; i++){
        String tmpStr = Integer.toHexString(cArray);
        if(tmpStr.length() < 4)
          if(tmpStr.length() == 3)
            tmpStr = "0" + tmpStr;
          else
          if(tmpStr.length() == 2)
            tmpStr = "00" + tmpStr;
          else
          if(tmpStr.length() == 1)
            tmpStr = "000" + tmpStr;
          else
            tmpStr = "0000";
        encodedName = encodedName + tmpStr;
      }

    }catch(MalformedInputException e){
      encodedName = pStr;
    }catch(UnsupportedEncodingException e){
      encodedName = pStr;
    }
    return encodedName;
  }
  /**
   *
   * @param str String
   * @return String
   */
  public static String getDecodedFileName(String str){
    if(str == null || str.length() <= 0)
      return null;
    int dot = str.lastIndexOf(46);
    String s1;
    String s2;
    if(dot >= 0){
      s1 = str.substring(0, dot);
      s2 = str.substring(dot);
    } else{
      s1 = str;
      s2 = "";
    }
    return fromUnicodeString(s1) + s2;
  }
  /**
   *
   * @param orgStr String
   * @return String
   */
  public static String fromUnicodeString(String orgStr){
    String result = "";
    if(orgStr != null && orgStr.length() > 0){
      int len = orgStr.length();
      if(len % 4 != 0){
        System.out.println("Invalid String, the length should be multiple of 4");
        return result;
      }
      for(int i = 0; i < orgStr.length() / 4; i++){
        String substr = orgStr.substring(i * 4, (i + 1) * 4);
        String tmpresult = getDecodedChar(substr);
        result = result + tmpresult;
      }

    }
    return result;
  }
  /**
   *
   * @param pStr String
   * @return String
   */
  private static String getDecodedChar(String pStr){
    int i = Integer.valueOf(pStr, 16).intValue();
    char cc = (char)i;
    String ss = String.valueOf(cc);
    return ss;
  }

  public static String toString(String s){
    if(s == null)
      return "";
    else
      return s;
  }

  public static boolean isEmpty(String string){
    return string == null || string.trim().length() == 0;
  }

  /**
   * split("separator",'a') returns {"sep","r","tor"}
   * @param src
   * @param char
   * @return        <br>
   */
  public static String[] split(String src, char separator){

    if (null != src || "".equals(src))
    {
            return src.split("separator");
    }
      return null;
  }

  public static int count(String ptr, char c){
    int coun=0, pos=0;
    while ((pos=ptr.indexOf(c,pos))!=-1){
      coun++;
      pos++;
    }
    return coun;
  }


  public static int count(String ptr, String c){
    int coun=0, pos=0;
    while ((pos=ptr.indexOf(c,pos))!=-1){
      coun++;
      pos+=c.length();
    }
    return coun;
  }

  /**
   *replace string<p>
   *exp:<br>
   *String buf = "this is an test";   <br>
   *String oldstr = "an";  <br>
   *String newstr = "a";  <br>
   *StringUtil.replace(buf,oldstr,newstr)  returns "this is a test" ;  <br>
   *public String replace(char oldChar,char newChar)  <br>
   *"mesquite in your cellar".replace('e', 'o')     returns "mosquito in your collar"  <br>
   *@param   buf     originally
   *@param   oldstr  old sub string
   *@param   newstr  new sub string
   *@return  new string   <br>
   */
   public static String replace(String buf, String oldstr, String newstr)
   {
     if(null != buf || "".equals(buf))
     {
             return buf.replaceAll(oldstr, newstr);
     }
     return null;
   }
   public static void main(String args[])
   {
           System.out.println(StringUtil.getEncodedFileName("fasdfasdfhfaklhs123123.fas"));
   }

   /**
    * 对字符串中的特殊字符"\n"加转义符"\\n"。
    *
    * @param   change[]    需转换的字符串数组
    * @return              转换后的字符串数组
    */


   public static String[]  changeNewLine(String[] change) {
       if(change != null) {
           int i = 0;
           int width = change.length;
           int index = -1;
           for (i = 0; i < width; i++) {
               index = change.indexOf("\n");
               while(index != -1) {
                   change = change.substring(0,index) + "\\n"
                               + change.substring(index + 1);
                   index = change.indexOf("\n");
               }
           }
           return change;
       }
       return change;
   }

   /**
    * 对字符串中的特殊字符"\n"加转义符"\\n"。
    *
    * @param   change[]    需转换的字符串数组
    * @return              转换后的字符串数组
    */


   public static String changeNewLine(String change) {
       if(change != null) {
           int i = 0;
           int index = -1;
               index = change.indexOf("\n");
               while(index != -1) {
                   change = change.substring(0,index) + "\\n"
                               + change.substring(index + 1);
                   index = change.indexOf("\n");
               }
           return change;
       }
       return change;
   }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值