java 怎么知道字符串的编码
收起
云计算小粉
2018-05-10 20:09:11
2844
0
2
条回答
写回答
取消
提交回答
-
只能逐一判断
String iso8859 = new String(sb.toString().getBytes("iso8859-1"));
String gbk = new String(sb.toString().getBytes("gbk"));
String utf8 = new String(sb.toString().getBytes("utf-8"));
if(iso8859.equals(sb.toString())){
System.out.println("iso8859");
}else if(gbk.equals(sb.toString())){
System.out.println("gbk");
}else if(utf8.equals(sb.toString())){
System.out.println("utf8");
}
2019-07-17 22:18:24
-
通过将字符串重新编码,循环判断是否相等来判断字符串的编码,比如boolean flag = str.equals(new String(str.getBytes(code), code))
2019-07-17 22:18:24