Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Unicode remove char from the string

I have a string formatted with NumberFormat instance. When i display the chars of the string i have a non-breaking space (hexa code : A0 and unicode 160). How can i remove this character from my string. I tried string = string.replaceAll("\u0160", ""); and string = string.replaceAll("0xA0", ""), both didn't work.

String string = ((JTextField)c)getText();
string = string.replace("\u0160", "");
System.out.println("string : " string);

for(int i = 0; i < string.length; i++) {
System.out.print("char : " + string.charAt(i));
System.out.printf("Decimal value %d", (int)string.charAt(i));
System.out.println("Code point : " + Character.codePointAt(string, i));
}

The output still contains a white space with decimal value 160 and code point 160.

like image 676
xtrem06 Avatar asked Dec 14 '11 07:12

xtrem06


3 Answers

The unicode character \u0160 is not a non-breaking space. After the \u there must be the hexadecimal representation of the character not the decimal, so the unicode for non-breaking space is \u00A0. Try using:

string = string.replace("\u00A0","");
like image 103
halex Avatar answered Nov 06 '22 00:11

halex


This is working as is.

public static void main(String[] args) {
    String string = "hi\u0160bye";
    System.out.println(string);
    string = string.replaceAll("\u0160", "");
    System.out.println(string);
}
like image 33
Daniel Moses Avatar answered Nov 06 '22 00:11

Daniel Moses


String string = "89774lf&933 k880990";

string = string.replaceAll( "[^\\d]", "" );

System.out.println(string);

OUTPUT:

89774933880990

It will eliminate all the char other than digits.

like image 43
dku.rajkumar Avatar answered Nov 05 '22 23:11

dku.rajkumar