六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 77|回复: 0

字符串帮助类

[复制链接]

升级  6.67%

14

主题

14

主题

14

主题

秀才

Rank: 2

积分
60
 楼主| 发表于 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[i++];      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();  } 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表