用str.equals(new String(str.getBytes(encode), encode))格式判断编码不准确的问题

ameyume
博客专家认证
2014-09-25 03:02:43
用下面一段代码判断中文编码不准确啊。
         String artist = "梁静茹";
System.out.println(artist);
System.out.println("encode is " + getEncoding(artist));
}

public static String getEncoding(String str) {
try {
String encode = "GB2312";
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = "ISO-8859-1";
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = "UTF-8";
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
encode = "GBK";
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (UnsupportedEncodingException e) {
}
return null;
}

这样输出的是GB2312,如果把GB2312那段代码注释掉,输出的就是UTF-8;如果把UTF-8的判断也注释掉,输出的就是GBK。
怎样判断才能准确呢?
...全文
288 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
teemai 2014-09-25
  • 打赏
  • 举报
回复

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧