Question:
How to remove repeated adjacent characters in a string?
Code:
public class RemoveRepeatedChars { public static void main(String[] args) { String str = "aabbccddeefffffg"; System.out.println(str); String regex = "(.)\\1{1,}"; str = str.replaceAll(regex,"$1"); System.out.println(str); } }
Output:
$ java RemoveRepeatedChars aabbccddeefffffg abcdefg