zoukankan      html  css  js  c++  java
  • 检测字符串编码格式

    方法如下,正确编码打印不会乱码(不区分大小写)

    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@参考文章

  • 相关阅读:
    2019/1/17 break语句小练习
    2019/1/17goto语句小试牛刀
    python 中* 和**的作用
    python 元组编码和解码问题
    python SMTP 发送邮件
    python 自定义异常
    python websocket client 使用
    excel、xls文件读写操作
    windows10局域网实现文件共享
    django入门
  • 原文地址:https://www.cnblogs.com/yanan7890/p/7098609.html
Copyright © 2011-2022 走看看