六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 386|回复: 0

判断一个字符串的编码格式,并进行转码

[复制链接]

升级  59.33%

39

主题

39

主题

39

主题

秀才

Rank: 2

积分
139
 楼主| 发表于 2013-2-8 01:02:59 | 显示全部楼层 |阅读模式
public class TranCharset {    /**     * 判断字符串的编码     *     * @param str     * @return     */    public static String getEncoding(String str) {        String encode = "GB2312";        try {            if (str.equals(new String(str.getBytes(encode), encode))) {                String s = encode;                return s;            }        } catch (Exception exception) {        }        encode = "ISO-8859-1";        try {            if (str.equals(new String(str.getBytes(encode), encode))) {                String s1 = encode;                return s1;            }        } catch (Exception exception1) {        }        encode = "UTF-8";        try {            if (str.equals(new String(str.getBytes(encode), encode))) {                String s2 = encode;                return s2;            }        } catch (Exception exception2) {        }        encode = "GBK";        try {            if (str.equals(new String(str.getBytes(encode), encode))) {                String s3 = encode;                return s3;            }        } catch (Exception exception3) {        }        return "";    }    public static void main(String[] args){    System.out.println(getEncoding("CSS测试"));    }}public static void main2(String[] args) throws Exception{        String desc="CSS测试";         try {            String b = new String(desc.getBytes("GBK"), "UTF-8");           System.out.println(b);        } catch (Exception e) {             e.printStackTrace();         }     }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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