栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 判断中文字符串的编码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 判断中文字符串的编码

1.判断中文字符串编码,返回编码

    public String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String code = encode;
                return code;
            }
        } catch (Exception e) {
            LOGGER.error("编码查询异常:{}", e);
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String code1 = encode;
                return code1;
            }
        } catch (Exception e1) {
            LOGGER.error("编码查询异常:{}", e1);
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String code2 = encode;
                return code2;
            }
        } catch (Exception e2) {
            LOGGER.error("编码查询异常:{}", e2);
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String code3 = encode;
                return code3;
            }
        } catch (Exception e3) {
            LOGGER.error("编码查询异常:{}", e3);
        }
        return "";
    }



public static void main(String[] args) 
{
    System.out.println( getEncoding("鎴戞槸涓浗浜"));
    System.out.println( getEncoding("D3D0D2BBCCECD4E7C9CFD0D1C0B4A3ACC8ABC7F2D2DFC7E9BDE1CAF8A3ACBDA1BFB5C2EBA3ACD0D0B3CCC2EBB9D8B1D5"));
    
}

2. 将编码结果打印到控制台

 

 不管梦想有多远,总有一天,我们还回到那个最初的地方!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号