检测字符串编码格式
方法如下,正确编码打印不会乱码(不区分大小写)
private static void testEncode(String str) throws UnsupportedEncodingException{
System.out.println(str+"----------------");
System.out.println(new String(str.getBytes(), "GB2312"));
System.out.println(new String(str.getBytes(), "UTF-8"));
System.out.println(new String(str.getBytes(), "GBK"));
System.out.println(new String(str.getBytes(), "ISO8859_1"));
System.out.println(new String(str.getBytes(), "ASCII"));
System.out.println(new String(str.getBytes(), "GB18030"));
System.out.println(new String(str.getBytes(), "UTF-16"));
System.out.println(new String(str.getBytes(), "Unicode"));
}
20200606:优化:
private void testEncode(String str) throws UnsupportedEncodingException{ Set names=Charset.availableCharsets().keySet(); for (Iterator iter = names.iterator(); iter.hasNext();) { String charsetName = (String) iter.next(); if(Charset.isSupported(charsetName)&&new String(str.getBytes(), charsetName).contentEquals(str)){ System.out.println(charsetName); } } }
此种方法是获取系统所有支持的charset@参考文章
本文来自博客园,作者:每天都要学一点,欢迎讨论和转载,转载请注明原文链接:https://www.cnblogs.com/yanan7890/p/7098609.html