Posted by: Sourav | November 7, 2014

Remove consecutive spaces including blanks in Java


class a{
public static void removeMultipleOccurrence(String input) {

input = input.replaceAll(“\\s+”, ” “);
input=input+” “;
char a;
System.out.println(input);
String finalValue = “”;
for (int i = 0; i < (input.length()-1); i++) {

if(!(input.charAt(i)==input.charAt(i+1)))
{
finalValue = finalValue + input.charAt(i);
}

}

System.out.println(finalValue);
}

public static void main(String args[])
{
removeMultipleOccurrence(“rohh hhitt t”);

}
}

Advertisements

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

Categories

%d bloggers like this: