xtrem06
xtrem06

Reputation: 253

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.

Upvotes: 12

Views: 31115

Answers (3)

dku.rajkumar
dku.rajkumar

Reputation: 18568

String string = "89774lf&933 k880990";

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

System.out.println(string);

OUTPUT:

89774933880990

It will eliminate all the char other than digits.

Upvotes: 4

halex
halex

Reputation: 16403

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","");

Upvotes: 47

Daniel Moses
Daniel Moses

Reputation: 5858

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);
}

Upvotes: 2

Related Questions