本文介绍了替换字符串中的非 ASCII 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字符串 A função
, Ãugent
在其中我需要替换像 ç
,ã
这样的字符,Ã
带有空字符串.
I have strings A função
, Ãugent
in which I need to replace character like ç
,ã
,Ã
with empty strings.
如何只匹配那些非 ASCII 字符?
How can I match only those non ASCII characters?
我正在使用一个函数
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;
}
但它不能正常工作......需要什么改进......这里我还有一个问题是最终字符串被空格字符替换,这在字符串中创建了额外的空间.
but it is not working properly..what improvement it is needed...here I have one more problem is that final string is getting replaced by space character which create the extra space in string.
推荐答案
这将搜索并替换所有非 ASCII 字母:
This will search and replace all non ASCII letters:
String resultString = subjectString.replaceAll("[^\x00-\x7F]", "");
这篇关于替换字符串中的非 ASCII 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!