Java program to remove whitespaces from the string

This program will show you how to remove whitespaces from a string using replaceAll function.

In this replaceAll function will replace all whitespace " " with "".

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(; System.out.println("Enter the string"); String str=sc.nextLine(); str = str.replaceAll("\\s", ""); System.out.println("String after removing white spaces- "+ str); } }