/**
* 将字符串转换成指定编码格式
*
* @param str
* @param encode
* @return
*/
public static String transcoding(String str, String encode) {
String df = "ISO-8859-1";
try {
String en = getEncode(str);
if (en == null)
en = df;
return new String(str.getBytes(en), encode);
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String getEncode(String str) {
byte[] data = str.getBytes();
byte[] b = null;
a:for (int i = 0; i < encodes.length; i++) {
try {
b = str.getBytes(encodes[i]);
if (b.length!=data.length)
continue;
for (int j = 0; j < b.length; j++) {
if (b[j] != data[j]) {
continue a;
}
}
return encodes[i];
} catch (UnsupportedEncodingException e) {
continue;
}
}
return null;
}
* 将字符串转换成指定编码格式
*
* @param str
* @param encode
* @return
*/
public static String transcoding(String str, String encode) {
String df = "ISO-8859-1";
try {
String en = getEncode(str);
if (en == null)
en = df;
return new String(str.getBytes(en), encode);
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String getEncode(String str) {
byte[] data = str.getBytes();
byte[] b = null;
a:for (int i = 0; i < encodes.length; i++) {
try {
b = str.getBytes(encodes[i]);
if (b.length!=data.length)
continue;
for (int j = 0; j < b.length; j++) {
if (b[j] != data[j]) {
continue a;
}
}
return encodes[i];
} catch (UnsupportedEncodingException e) {
continue;
}
}
return null;
}
本文介绍了一种用于检测和转换字符串编码的方法。通过检测输入字符串的实际编码格式,该方法能够将其转换为所需的编码格式,这对于处理来自不同源的文本数据特别有用。
1023

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



