java 获取字符串编码格式_输入字符串的格式要求

java 获取字符串编码格式_输入字符串的格式要求大家好 又见面了 我是你们的朋友全栈君 判断一个字符串的编码格式 public static String getEncoding String str String encode GB2312 try if isEncoding str encode 判断是不是 GB2312 return encode catch

大家好,又见面了,我是你们的朋友全栈君

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

    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;
}
编程小号
上一篇 2025-07-16 23:27
下一篇 2025-06-28 16:46

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/129383.html