如何从字符串中删除非 ASCII 字符?
我有字符串,其中我需要用空字符串替换像,和这样的字符。"A função"
"Ãugent"
ç
ã
Ã
如何从字符串中删除这些非 ASCII 字符?
我尝试使用以下函数实现它,但它无法正常工作。一个问题是不需要的字符被空格字符替换。
public static String matchAndReplaceNonEnglishChar(String tmpsrcdta) {
String newsrcdta = null;
char array[] = Arrays.stringToCharArray(tmpsrcdta);
if (array == null)
return newsrcdta;
for (int i = 0; i < array.length; i++) {
int nVal = (int) array[i];
boolean bISO =
// Is character ISO control
Character.isISOControl(array[i]);
boolean bIgnorable =
// Is Ignorable identifier
Character.isIdentifierIgnorable(array[i]);
// Remove tab and other unwanted characters..
if (nVal == 9 || bISO || bIgnorable)
array[i] = ' ';
else if (nVal > 255)
array[i] = ' ';
}
newsrcdta = Arrays.charArrayToString(array);
return newsrcdta;
}