Write a Java Program to Remove All Whitespaces from the String

January 24, 2023 0 Comments

In this tutorial learn how you can write a program to remove all whitespaces from the String in java. We can use both float and double classes.

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Write a Program to Remove All Whitespaces from the String in Java

Here is the code to remove all whitespaces from the String in Java programming.

//Write a program to remove all whitespaces from the String.
import java.util.Scanner;

public class Whitespace {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence with a whitespaces");
        String name = input.nextLine();
        String word = name.replaceAll("\\s", "");
        System.out.println("When we remove whitespaces from " + name + " we will get " + word + ".");
        input.close();

    }
}

Test Live At Jdoodle

Conclusion

This is how you can write a program to remove all whitespaces from the String in java programming. Comment below if you like our tutorial.

Leave a comment

Gallery