如何查看字符编码类型

如题所述

可以通过以下方法来进行编码格式判断,输入一个字符串,之后返回字符串编码类型。
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
String s = encode;
return s; //是的话,返回“GB2312“,以下代码同理
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return ""; //如果都不是,说明输入的内容不属于常见的编码格式。

(望楼主采纳哦)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-03
基本上现在的字符集 MySQL 都支持,查看 MySQL 支持的字符集列表, 有两种方法:
1. SQL 语句
2. 查看元数据字典表
查询结果:
1)第一列代表字符集名字;
2)第二列表示字符集排序规则;
3)第三列表示字符集描述;
4)第四列表示字符集编码的最大字节数。
相似回答