码迷,mamicode.com
首页 > 编程语言 > 详细

Java工具类-转换字符编码

时间:2018-11-06 22:22:10      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:编码   tostring   public   工具类   except   ==   system   print   java   

package common;
/**
 *字符串处理公用类 
 */
public class DealString {
    /**
     * 转换字符编码 由“iso-8859-1”西文转换为简体中文
     */
    public static String toGb(String uniStr){
        String gbStr="";
        if(uniStr==null){
            uniStr="";
        }
        try{
            byte[] tempByte=uniStr.getBytes("ISO8859_1");
            gbStr=new String(tempByte,"GB2312");
        }
        catch(Exception ex){
            System.out.println(ex.toString());
        }
        return gbStr;
    }
    /**
     * 把字符串转化为uincode编码
     * @param gbStr
     * @return
     */
    public static String toUni(String gbStr){
        String uniStr="";
        if(gbStr==null){
            gbStr="";
        }
        try{
            byte[] tempByte=gbStr.getBytes("GB2312");
            uniStr=new String(tempByte,"ISO8859_1");
        }
        catch(Exception ex){
            
        }
        return uniStr;
    }
    /**
     * 去掉字符串的单引号,例如 输入a‘s将输出a1s以便把包含单引号的字符串插入数据库
     * 不报错
     */
    public String dbEncode(String str){
        if(str==null){
            str="";
        }else{
            try{
                str=str.replace(‘\‘‘,(char) 1).trim();
            }
            catch(Exception e){
                System.err.println(e.getMessage());
                e.printStackTrace();
                return str;
            }
        }
        return str;
    }
}

 

Java工具类-转换字符编码

标签:编码   tostring   public   工具类   except   ==   system   print   java   

原文地址:https://www.cnblogs.com/Roni-i/p/9918079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!