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 "".

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