Java program to remove all white space from a string

Java program to remove all white space from a String :

In this tutorial, we will learn how to remove all blank characters (like whitespace,tabs,newline etc) from a string. We will learn two different methods to achieve this.

Let’s take a look :

Using replaceAll method :

The replaceAll method is defined as :

public String replaceAll(String regex, String replacement)

It matches all sub-string of the caller string with the input regex and replace it with replacement string. In place of regex, we will pass \s and in place of replacement, we will pass empty String "". \s matches all whitespace characters. You can verify it in any website like regex101.

regex

So, all blank characters will replace with the empty string after program execution complets.

Let’s take a look into the example program :

Java Program using replaceAll :

public class Main {

    private static final String INPUT_STRING = "This is a string  with \t tab and \n new line";

    public static void main(String[] args) throws java.lang.Exception {
        String finalString = INPUT_STRING.replaceAll("\\s","");
        System.out.println("Original String : "+INPUT_STRING);
        System.out.println("Final String : "+finalString);
    }

}

Output :

Original String This is a string  with    tab and 
 new line
Final String : Thisisastringwithtabandnewline

Using a loop :

We can also remove all blank spaces using a loop. Let’s take a look into the program first :

Java Example Program :

public class Main {

    private static final String INPUT_STRING = "This is a string  with \t tab and \n new line";

    public static void main(String[] args) throws java.lang.Exception {

        //1
        StringBuilder strBuilder = new StringBuilder();

        //2
        for (int i = 0; i < INPUT_STRING.length(); i++)
        {
            //3
            if(!Character.isWhitespace(INPUT_STRING.charAt(i)))
            {
                //4
                strBuilder.append(INPUT_STRING.charAt(i));
            }
        }
        
        //5
        String finalString = strBuilder.toString();

        //6
        System.out.println("Original String : "+INPUT_STRING);
        System.out.println("Final String : "+finalString);

    }

}

Output :

Original String : This is a string  with      tab and 
 new line
Final String : Thisisastringwithtabandnewline

Explanation :

The commented numbers in the above program denotes the step number below :

  1. Create one StringBuilder object to store the final result string.

  2. Start one for loop , that will run same as the length of the string.

  3. Scan each character. First, we are taking the character for that position using charAt method. Then we are checking if it is a blank character or not using isWhiteSpace method.

  4. If a character is not a blank character, append it to the StringBuilder object.

  5. Finally, convert the StringBuilder object to string using toString method.

  6. Print out both strings.

Similar tutorials :