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.
Leave a Reply