Java program to remove all whitespaces from a string

www‮figi.‬tidea.com

Here's an example Java program that removes all whitespace characters from a string:

public class RemoveWhitespaceFromString {
    public static void main(String[] args) {
        String input = "This is a string with   whitespace.";

        // Remove all whitespace characters from the string
        String output = input.replaceAll("\\s", "");

        System.out.println("Input string: " + input);
        System.out.println("Output string: " + output);
    }
}

This program uses the replaceAll() method to replace all whitespace characters in the input string with an empty string. The regular expression \s matches any whitespace character, including spaces, tabs, and newlines. The resulting string is then printed to the console.

The output of this program would be:

Input string: This is a string with   whitespace.
Output string: Thisisastringwithwhitespace.