您的位置:首页 > 编程语言 > Java开发

java 获取字符串的编码

2014-12-30 22:51 369 查看

文章来源:http://blog.sina.com.cn/s/blog_4b8abbd60100yqkh.html

public String getEncoding(String str) {

String encode = "GB2312";

try {
// 将str转换成gb2312编码的字节,并转换成gb2312编码的字符串

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 "";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: