Remove Whitespace

Remove all whitespace characters from a string.

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.nextLine();

        String noSpace = s.replaceAll("\\s+", "");
        System.out.println("Without whitespace: " + noSpace);
        sc.close();
    }
}

Output

Enter a string: a b  c
Without whitespace: abc

We use a regex \s+ to match all whitespace and replace with empty string.