//判断字符串的编码格式
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return s;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return "";
}
//将不同编码格式转为UTF-8显示出来
public static String toChinese(String strvalue, String encodeFormat) {
try {
if (strvalue == null) {
return "";
} else {
strvalue = new String(strvalue.getBytes(encodeFormat), "UTF-8").trim(); return strvalue;
}
} catch (Exception e) {
return "";
}
}
【java代码段】判段字符串的编码格式并转为UTF-8的编码格式
Java编码转换相关内容
最新推荐文章于 2025-09-19 16:37:32 发布
博客围绕Java编码转换展开,虽未给出具体内容,但可知核心聚焦于此信息技术领域的关键操作,编码转换在Java开发中至关重要,能解决字符显示等问题。
1360

被折叠的 条评论
为什么被折叠?



