Java——获取字符串编码格式


 判断一个字符串的编码格式: 

    public static String getEncoding(String str) { String encode = "GB2312"; try { if (isEncoding(str, encode)) { // 判断是不是GB2312 return encode; } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1 return encode; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (isEncoding(str, encode)) { // 判断是不是UTF-8 return encode; } } catch (Exception exception2) { } encode = "GBK"; try { if (isEncoding(str, encode)) { // 判断是不是GBK return encode; } } catch (Exception exception3) { } return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。 } public static boolean isEncoding(String str, String encode) { try { if (str.equals(new String(str.getBytes(), encode))) { return true; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return false; }



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM