How to remove all white spaces from String in java

In this post, we will see how to remove all white spaces from String in java.

There are multiple ways to remove spaces from String.

  1. Using ReplaceAll
  2. Using Iteration

Using ReplaceAll :

You can simply call ReplaceAll method to remove white spaces as shown below.

Using iteration :

You can iterate over String using charAt and check if Character is whitespace or not.

Java Program to remove all white spaces from String

When you run above program, you will get below output:

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *