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

Similar Program

# Program Language
1 Java
2 Java
3 Java
4 Java
5 Java
6 Java
7 Java
8 Java
9 Java
10 Java
11 Java
12 Java
13 Java
14 Java
15 Java
16 Java
17 Java
18 Java
19 Java
20 Java
# Program Language