January 1, 2023 Replace Multiple Consecutive Characters with Single Character in Java This tutorial shows you how to replace multiple consecutive characters with single character. Consider we have an input as: String str = "%%New%%%%"; Here, there are multiple consecutive occurrence of % character and we can replace its each set of consecutive occurrence as shown below: str = str.replaceAll("(.)\\1+","$1"); So, after performing the above replace operation, the resultant string is %New%. Note: The above replace operation will handle any character multiple occurrence at any position in the string. Like this:Like Loading... Related Java Java Interview Question Replace Multiple Consecutive Characters with Single Character in Java