rahulsri rahulsri - 10 months ago 58
Java Question

Replace non ASCII character from string

I have strings

A função
in which I need to replace character like
with empty strings.

How can I match only those non ASCII characters?

i am using a function

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 = Character.isISOControl(array[i]); // Is character ISO control
boolean bIgnorable = Character.isIdentifierIgnorable(array[i]); // Is Ignorable identifier
// 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.


This will search and replace all non ASCII letters:

String resultString = subjectString.replaceAll("[^\\x00-\\x7F]", "");