, asked
Views : 4

How to remove all whitespace from String in Java?

I have a String with white spaces how to remove these white spaces from this string? Please explain with an example.

1 Answer
Anjana

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.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Hi how are you Welcome to Tutorialspoint.com");
      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll(" ", ""));
   }
}

Output

Return Value :HihowareyouWelcometoTutorialspoint.com

Advertisements
Related Questions