Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the  character from a string in java?

I have the following string in which the  special character is coming in hidden. I want to remove only the  from this string ~IQBAL~KARACHI¦~~~~~~~~~~~.

Here is a before and after image to show what I mean:

enter image description here

I've tried this code:

responseMessageUTF.replaceAll("\\P{InBasic_Latin}", "");

but this is also replacing the ¦ character. Is there any way to remove only the  character and not the ¦ character?

like image 867
Ahmed Junaid Avatar asked Dec 13 '25 06:12

Ahmed Junaid


1 Answers

I have a simple one liner code, it removes for most of the non-UTF-8 characters. I tested for your character as well i.e. Â.

        String myString = "~KARACHI¦~~~~~~";
        String result = myString.replaceAll("[^\\x00-\\x7F]","");
        System.out.println(result);

You can find complete code here. You may test that as well here.

like image 199
Mahesh Kapoor Avatar answered Dec 15 '25 18:12

Mahesh Kapoor