Share on Google+Share on Google+

Remove Repeated Characters from the String

In this java Tutorial Series, we are going to remove repeated characters from the string.

Remove Repeated Characters from the String

In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter the string. The class BufferedReader reads the string from the console. Then we have used the replaceAll((String.format("(.)(?<=(?:(?=\\1).).{0,%d}(?:(?=\\1).))" method that have removed all the repeated characters.

Here is the code:


public class RemoveRepeatedCharacters{
public static void main(String[]argsthrows Exception{
System.out.print("Enter the String : ");
BufferedReader br =new BufferedReader(new InputStreamReader(;
String inputString = br.readLine();
System.out.println("Original String is: "+inputString);
inputString = inputString.replaceAll((String.format("(.)(?<=(?:(?=\\1).).{0,%d}(?:(?=\\1).))", inputString.length()))"");
System.out.println("New String is: "+inputString);


Enter the String : Hello World
Original String is: Hello World

New String is: Helo Wrd


Posted on: October 26, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null