How to remove all whitespace from String in Java?



In this article, we will learn how to remove all whitespace from a string in Java. For example, if the given string contains white spaces as shown below -

Initial String = "Hi how are you Welcome to Tutorialspoint.com"

The output should contain a string without white spaces as -

String without whitespaces = "HihowareyouWelcometoTutorialspoint.com"

Using replaceAll() Method

The replaceAll() method replaces substrings that match a given regular expression with a specified string. By passing white spaces and an empty string as parameters to this method, we can remove all whitespaces from the input string.

Steps to remove whitespaces from a string

The following are the steps to remove whitespaces from a string using the replaceAll() method:

  • Create a string containing some whitespaces.
  • Use the replaceAll() method to replace spaces with an empty string.
  • Print the output string to display the result without whitespaces.

Example

The following is an example of removing all whitespaces from a string using the replaceAll() method:

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(" ", ""));
   }
}

Following is the output of the above program -

Return Value :HihowareyouWelcometoTutorialspoint.com

Using replace() Method

Now, let's see how to remove all whitespaces from a string in Java using the replace() method. This method replaces all occurrences of a specified character with another character or an empty string.

Steps to remove whitespaces from a string

The following are the steps to remove whitespaces from a string using the replace() method:

  • Create a string containing some whitespaces.
  • Use the replace() method to replace spaces with an empty string.
  • Print the output string to display the result without whitespaces.

Example

The following is an example of removing all whitespaces from a string using the replace() method:

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.replace(" ", ""));
   }
}

Following is an output of the above program -

Return Value :HihowareyouWelcometoTutorialspoint.com

Using Streams

We can also remove all whitespaces from a string using Java Streams. This approach allows us to process the string in a functional programming.

Steps to remove whitespaces from a string

The following are the steps to remove whitespaces from a string using Streams:

  • Create a string containing some whitespaces.
  • Convert the string to a stream of characters.
  • Filter out the whitespace characters.
  • Collect the remaining characters into a new string.
  • Print the output string to display the result without whitespaces.

Example

The following is an example of removing all whitespaces from a string using Streams:

import java.util.stream.Collectors;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Hi how are you Welcome to Tutorialspoint.com");
      String result = Str.chars()
                         .filter(c -> !Character.isWhitespace(c))
                         .mapToObj(c -> String.valueOf((char) c))
                         .collect(Collectors.joining());
      System.out.print("Return Value :" );
      System.out.println(result);
   }
}

Following is the output of the above program -

Return Value :HihowareyouWelcometoTutorialspoint.com
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-24T13:03:06+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements