本文介绍了如何从字符串中删除非 ASCII 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字符串 "A função"
, "Ãugent"
其中我需要替换像 ç
, 这样的字符ã
和 Ã
带有空字符串.
I have strings "A função"
, "Ãugent"
in which I need to replace characters like ç
, ã
, and Ã
with empty strings.
如何从我的字符串中删除那些非 ASCII 字符?
How can I remove those non-ASCII characters from my string?
我已尝试使用以下函数实现此功能,但无法正常工作.一个问题是不需要的字符被空格字符替换.
I have attempted to implement this using the following function, but it is not working properly. One problem is that the unwanted characters are getting replaced by the space character.
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;
}
推荐答案
这将搜索并替换所有非 ASCII 字母:
This will search and replace all non ASCII letters:
String resultString = subjectString.replaceAll("[^\x00-\x7F]", "");
这篇关于如何从字符串中删除非 ASCII 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!