Search code examples
javaintellij-ideareplacespecial-characters

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


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,


Solution

  • 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.