How to remove all whitespace from String in Java?

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


Live Demo

public class Test {
   public static void main(String args[]) {
      String Str = new String("Hi how are you Welcome to");
      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll(" ", ""));


Return Value

Updated on: 26-Feb-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started