How to Remove Spaces in a String in Java


In this example we will show the method to remove spaces in a string in Java.

Source Code

package com.beginner.examples;

public class RemoveSpaces {

  public static void main(String[] args) {
    String str = "ABC DE  F";
    str = str.replaceAll(" +", ""); // remove spaces
    System.out.println(str);
  }
}

Output:

ABCDEF
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments