Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Remove all the White Space from the String

In this tutorial, we will learn how to remove the whitespaces from a string. This can be done by first traversing the string and then checking if any character of the string is matched with a white-space character or not.

If anything is matched, then any built-in method like replace() can be used to remove the white space. This can also be done by using loops. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the String: Study Tonight

Output: The entered string after removal of white spaces is: StudyTonight

Let us look at the examples for better understanding.

Program 1: Remove White Spaces from a String

In this program, we will see how to remove all the whitespace from a string when the string is user-defined. Here, we will ask the user to enter a string and then we will remove the whitespaces from the string.

Algorithm

  1. Start

  2. Declare a string

  3. Ask the user to initialize the string.

  4. Use the replaceAll to remove the regular expression \\s that finds all white space characters (tabs, spaces, newline characters, etc.) in the string with ""(empty space literal).

  5. Print the string

  6. Stop

Below is the Java code to remove white space from the String.

//Java Program to remove all the whitespace from a string
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
         //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the String :");
        String str=sc.nextLine();    //Initialize the String
        
        str = str.replaceAll("\\s", ""); 
       
        System.out.println("After Removing Whitespaces the entered string is:"+str); 
     }
}


Enter the String: Study Tonight
After Removing Whitespaces the entered string is: StudyTonight

Program 2: Remove White Spaces from a String

In this program, we will see how to remove all the whitespace from a string when the string is pre-defined in the program.

Algorithm

  1. Start

  2. Declare a string

  3. Initialize it.

  4. Use the replaceAll to remove the regular expression \\s that finds all white space characters (tabs, spaces, newline characters, etc.) in the string with ""(empty space literal).

  5. Print the string

  6. Stop

Below is the Java code to remove white space from the String.

//Java Program to remove all the whitespace from a string
public class Main
{
     public static void main(String []args)
     {
        String str="Study   Tonight ";
        System.out.println("The entered string is: "+str);        
        str = str.replaceAll("\\s", ""); 
        System.out.println("After Removing Whitespaces the entered string is: "+str); 
     }
}


The entered string is: Study Tonight
After Removing Whitespaces the entered string is: StudyTonight

Program 3: Remove White Spaces from a String in Java

In this program, we will see how to remove all the whitespace from a string when the string is user-defined. Here, we will ask the user to enter a string and then we will remove the whitespaces from the string by converting it to a char array.

Algorithm

  1. Start

  2. Declare a string

  3. Ask the user to initialize the string.

  4. Convert the string to a character array

  5. Declare a StringBuffer variable.

  6. Use a for loop to iterate through all the characters of the string

  7. Use an if condition to check for white spaces.

  8. If any are found, then use the append string.

  9. Convert a string buffer to a string

  10. Print the string.

  11. Stop

Below is the Java code to remove white space from the String.

//Java Program to remove all the whitespace from a string
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the String :");
        String str=sc.nextLine();    //Initialize the String
        
        char[] strArray = str.toCharArray();  
        StringBuffer stringBuffer = new StringBuffer();  
        for (int i = 0; i < strArray.length; i++) 
        {  
            if ((strArray[i] != ' ') && (strArray[i] != '\t')) 
            {  
                stringBuffer.append(strArray[i]);  
            }  
        }  
        //Print the string after the removal of white space
        String noSpaceStr2 = stringBuffer.toString();  
        System.out.println("Removing all the white space from the string is: "+noSpaceStr2);  
        
     }
}


Enter the String: Example of Removing White Space
Removing all the white space from the string is: ExampleofRemovingWhiteSpace