I am trying to replace all duplicate characters from String in Java and allow only one.
For example:
aaaaa ---> a
For this, I tried using the replaceAll method:
"aaaaa".replaceAll("a*","a") //returns "aa"
I developed a recursive method that is probably not very efficient:
public String recursiveReplaceAll(String original,String regex, String replacement) { if (original.equals(original.replaceAll(regex, replacement))) return original; return recursiveReplaceAll(original.replaceAll(regex, replacement),regex,replacement); }
This method works, I'm just wondering if there is anything using RegEx, for example, that does the job with better performance.
java regex recursion
Mayday
source share