public class RemoveDuplicates {
public static void main(String[] args) {
String s1 = "This is a string with spaces.";
String s2 = s1.replaceAll("\\s+", " ");
System.out.println(s1);
System.out.println(s2);
}
}
Output:
$ java RemoveDuplicates This is a string with spaces. This is a string with spaces.