How to remove duplicated special char in Java? [closed]

i have String in java: “[email protected]}Cl~Aa@pO}[email protected]|TkJjSoW|[email protected]}[email protected]_@j[email protected]{[email protected][email protected]{\t][email protected]@[email protected]

And i would like to change all duplicates “\” to one “”.

Replace function does not work on special characters 🙁 How can i do that?

Answer

I think replace("\\", "\") should replace all backlashes:

String input = "[email protected]}[email protected]}[email protected]|TkJjSoW|[email protected]}[email protected][email protected]@{[email protected][email protected]{\\t][email protected]@[email protected]\\";
System.out.println("input: " + input);
String replaced = input.replace("\\", "\");
System.out.println(replaced);

Above code prints the following output: