Java Program to remove all white spaces from a String.

Java 8Object Oriented ProgrammingProgramming

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

 Live Demo

public class DuplicateCharacters {
   public static void main(String[] args){
      String str = "Hello how are you";
      str = str.replaceAll(" ", "");
      System.out.println(str);
   }
}

Output

Hellohowareyou
raja
Published on 12-Feb-2018 14:28:27
Advertisements