java string replace multiple occurrences of a character

 

To replace multiple occurrences of a character in a string use following regular expression in your java code:

String s = "helloooooo";
s = s.replaceAll("(.)\\1+","$1");
System.out.println(s);

In above code input string helloooooo is converted as hello

Output:

hello

Advertisements
By Sandeep Posted in Java Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s