Java program to remove all the white spaces from a given string

Java8Java Programming Object Oriented Programming

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with ".

Example

import java.io.*;
import java.util.Scanner;
public class RemovingWhiteSpaces {
   public static void main(String args[]) {
      System.out.println("Enter a string value ::");
      Scanner sc = new Scanner(System.in);
      String str = sc.nextLine();
      System.out.println(str.replaceAll(" ", ""));
   }
}

Output

Enter a string value ::
Hi welcome to tutorialspoint
Hiwelcometotutorialspoint
raja
Published on 26-Apr-2018 12:50:16
Advertisements