I have tried to replace all "|" by "2" using following code:
String myString = "I want to change | with 2";
String trimedString = myString.replaceAll("|", "2");
System.out.print(trimedString);
My expected output was:
"I want to change | with 2"
But real output was:
"2I2 2w2a2n2t2 2t2o2 2c2h2a2n2g2e2 2|2 2w2i2t2h2 222"
It added 2 before and after each char. What is the correct way of doing this?
You have to insert two backslashes in front of the pipe-symbol in order to escape it.
String myString = "I want to change | with 2";
String trimedString = myString.replaceAll("\\|", "2");
System.out.print(trimedString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With