Home > Enterprise >  How to eliminate the special character ^\^@ from String in Java
How to eliminate the special character ^\^@ from String in Java

Time:06-02

My application is reading a file which contains following data:

MENS HEALTH^\^@ P

while actual text should be

MENS HEALTH P

I have already replaced the '\u0000' but still "^\" is still remaining in the string. I am not sure what is code for this characters, so I can replace it.

When I open the file in intelliJ editor it's displayed as FS symbol.

Please suggest how I can eliminate this.

Thanks,

CodePudding user response:

Rather than worry about what characters the junk consists of, remove everything that isn't what you want to keep:

str = str.replaceAll("[^\\w ] ", "");

This deletes any characters that are not word characters or spaces.

CodePudding user response:

You can use a regular expression with String.replaceAll() to replace these characters.

Note that backslash has a special meaning and need to be escaped (with a backslash).

"my\\^@String".replaceAll("[\\\\^@]", "");

Online Demo

  • Related