javne 发表于 2013-1-27 04:47:46

字符串帮助类

/**          * 将GBK转化为ISO-8859-1          * @param para          * @return          */          public static String gbkToIso(String str) {               try {                   return new String(str.getBytes("GBK"), "ISO-8859-1");               } catch (UnsupportedEncodingException e) {                   return "";               } catch (Exception e) {                   return "";               }         }        
/**          * 将字符串里的中文转码          * @param url          * @return          */          public static String enCodeString(String str){               if(!StringUtil.isFine(str))                   return null;               Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);               Matcher m = pattern.matcher(str);               if(m.find()){                   int get=m.start();                   String suburl=str.charAt(get)+"";                   String encodesuburl="";                   try{encodesuburl=URLEncoder.encode(suburl,"utf-8");}catch(Exception e){}                   str=str.replaceAll(suburl,encodesuburl);                   str=enCodeString(str);               }               str=str.replaceAll(" ","20%");               return str;         }    编码特殊的HTML字符为防止页面出错
public static String toHTML(String s){    char c[] = s.toCharArray();    char ch;    int i = 0;    StringBuffer buf = new StringBuffer();    while (i < c.length)    {      ch = c;      if (ch == '"')            buf.append(""");      else if (ch == '&')       buf.append("&");      else if (ch == '<')       buf.append("<");      else if (ch == '>')       buf.append(">");      else if (ch == '\'')       buf.append("'");      else      buf.append(ch);    }    return buf.toString();} 
页: [1]
查看完整版本: 字符串帮助类