java 獲取獲取字符串編碼格式

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 ""; //若是都不是,說明輸入的內容不屬於常見的編碼格式。
---------------------
做者:微wx笑
來源:CSDN
原文:https://blog.csdn.net/testcs_dn/article/details/53982619
版權聲明:本文爲博主原創文章,轉載請附上博文連接!編碼

相關文章
相關標籤/搜索