The strings are created/declared in Java to store the textual data. Strings provide an immutable character sequence that is utilized while coding in Java. In order to declare a string in Java, an object for the string is created initially. After the object is declared, various methods and functions are implemented according to the needs of the program. We may come across certain scenarios in Java where we need to remove the whitespaces from the specified string. To deal with such a situation, several approaches are utilized in Java.
In this write-up, we will demonstrate different methods to remove all the whitespaces in Java.
How to Remove/Eliminate All White Spaces From a String in Java?
Java is a programming language that offers several solutions for a particular problem. The built-in methods in Java provide effective solutions for problems like removing the whitespaces from the declared string. Some of them are listed below:
- Using String class
- Using the Character class
- Using String Buffer
- Using replace() method
Now let us discuss each method separately.
Method 1: Using the String Class
The replaceAll() method of the String class in Java is used to delete all the whitespaces from the declared string as depicted below.
class SpaceExample1 {
public static void main(String[] args)
{
String s = " Eliminate White Spaces in the string ";
System.out.println("The String is :" + s);
//Use the method of the string class
s = s.replaceAll("\\s", "");
//Print the results
System.out.println("The String without Whitespaces is: " + s);
}
}
In the above code block:
- The class is declared as “SpaceExample1” in Java.
- Next, the string is declared as “s”.
- The replaceAll() method of the String class uses the single space unicode “\\s” to remove all the white spaces in the string.
- The output is printed in the concatenated form.
Output
The output snap depicts that the strings are displayed without any whitespaces.

Method 2: Using the Character Class of Java
The Character.isWhitespace() method checks for any spaces declared within the string. We will remove the white spaces and return the concatenated string as depicted in the code below
// Import the required packages
import java.io.*;
import java.util.*;
//Declare the class
class SpaceExample2 {
public static void main(String[] args)
{
String sp = " 'Second Method to Remove White Spaces '";
System.out.println("The String is: " + sp );
String result = "";
//for loop to go through each and every character in the string with the charAt() method
for (int j = 0; j < sp.length(); j++) {
char chr = sp.charAt(j);
// Check for whitespace in the string
if (!Character.isWhitespace(chr)) {
result += chr;
}
}
//Print the results
System.out.println("The String without Whitespaces is " + result);
}
}
In the above code block:
- The packages are imported and the classes are declared.
- In the second step, the string is declared along with the whitespaces.
- The second string is declared as “result” which consists of whitespaces.
- The for loop, loops through the entire length of the characters declared in the given string.
- The if statement in the next step checks whether the specified character is a white space in the strings using the Character.isWhite() method.
- Finally, the outcome is displayed using the println() method.
Output
The output below shows that the first string is printed with space characters and in the next step the string is printed without the whitespaces in Java.

Method 3: Using String Buffer in Java
The String buffer class in Java creates strings that can be modified. The code below implements the append() method of the String buffer class to append all the characters other than whitespaces.
public class SpaceExample3 {
public static void main(String[] args) {
String str1 = " 'The Example for White spaces' ";
System.out.println("The String is:" + str1);
//Declare a character array
char[] array1 = str1.toCharArray();
//Create an object for the string buffer
StringBuffer strbuffer = new StringBuffer();
//Run a for loop
for (int i = 0; i < array1.length; i++) {
if ((array1[i] != ' ') && (array1[i] != '\t')) {
//append all the elements without white spaces
strbuffer.append(array1[i]);
}
}
String output = strbuffer.toString();
//Print the results
System.out.println("The String without White spaces is:" + output);
}
}
In the above code block:
- The string “str1” is declared along with the whitespaces.
- The toCharArray() method converts the given strings into the character array.
- Next, an object is created as a “strbuffer” for the buffer.
- The for loop iterates over the whole length of the declared string.
- The if statement checks and validates if there are whitespaces/tabs in the string or not.
- If the string is without the whitespaces, each character in the string is appended using the append() method of the String Buffer class.
- The “output” is converted to the string format using the toString() method and printed on the screen.
Output
The output below shows that the original string along with whitespaces is printed and the string without whitespaces is also printed.

Method 4: Using the replace() Method
The replace() method in Java replaces all the specified matches from the string. In the code below, we have specified our replace target as the spaces therefore, all the spaces are removed.
public class RemoveExample4 {
public static void main(String[] args) {
String s = " 'Method to Remove WhiteSpaces' ";
System.out.println("The Original String is:" + s);
System.out.println("The String Without Whitespaces is: " + s.replace(" ", "") );
}
}
In this code snippet:
- A string is created as “s” in the main() method.
- The original string with whitespaces is printed on the screen.
- In the last step, the replace() method replaces all the blank spaces between the strings, and the output gets printed on the screen.
Output
The output below shows that all the characters are printed in concatenated form using the replace() method where all the whitespaces are removed.

Some other methods are listed below which will assist you in removing the whitespaces from the beginning and end or from both.
Bonus Tip 1: The trim() Method
The code below implements the trim() method to remove the whitespaces from the very beginning and end of the declared string.
public class RemoveExample {
public static void main(String[] args) {
String s = " Method to Remove WhiteSpaces ";
System.out.println("The Original String is: \"" + s + "\"");
System.out.println("The String Without Whitespaces is:\"" + s.trim() + "\"");
}
}
The whitespaces in the above-declared string from the start and the end are removed using the trim() method.
Output
The below outcome demonstrates that the whitespaces are removed from the resultant string.

Bonus Tip 2: The strip() Method
The strip() method in Java removes the leading as well as trailing spaces from the declared string as implemented in the code below.
public class RemoveExample {
public static void main(String[] args) {
String s = " Method to Remove WhiteSpaces ";
System.out.println("The Original String is: \"" + s + "\"");
System.out.println("The String Without Whitespaces is:\"" + s.strip() + "\"");
}
}
In the above code block:
- The original string is declared and then printed.
- The strip method of Java removes the whitespaces from the beginning of the declared string and from the end of the string.
Output
The output below shows that the whitespaces have been successfully trimmed from the beginning and the end of the string.

This sums up the implementation of various ways to remove all the whitespaces from a specified string in Java.
Conclusion
There are different ways to remove the whitespaces from the declared string in Java. The different ways are implemented using built-in methods or a class in Java. For instance, the replaceAll() Method of the String class, the Character class, the String Buffer, and the replace() method, are used to remove all the spaces from a specific string. Among these, the replaceAll() method is quite effective in removing the whitespaces from a string.