The main problem with your code is that you need to assign the String returned from calling replace()
:
str = str.replace("||", "|");
In java, Strings are immutable - they can't be changed. String manipulation methods return a ^new String*, so to have any effect you must assign the returned value back to a variable - typically the same variable that held the String before the call.
Also, don't use replaceAll()
, which uses regex for its search term. Instead use replace()
, which still replaces all occurrences, but uses plain text for its search term. In regex, the pipe character has special meaning, so you'd need to escape each of them, which makes unnecessarily ugly code.