Java Program to remove white spaces from String

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Program to remove white spaces

In this article we will see that how to remove  white spaces form a string .

Concept :

Whitespace is a character which creates space on a page but  not a  visible mark. Common whitespace characters are  including  tabs and spaces.  Removing all white space will result no gap between the words or character .

We will see 3 different ways to remove white spaces from a string.

Method 1 : Java Program to remove white spaces using for loop

We can remove all white space in a string by using for loop .

 Approach :

  • Enter a string .
  • Take a blank string .
  • Take a for loop for checking every character in the string .
  • For every character in entered string ,if the character is a blank space / white space don’t include in the new string & if the character is not a white space then include in the newly created string .
  • Print the newly created string.

Program :

import java.util.Scanner;
public class Main {  
    public static void main(String[] args)
    {  
        // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter a string : ");  
        String s= sc.nextLine();   
        // CONVERTING TO CHAR. ARRAY 
        char[] s1 = s.toCharArray();  
        // CREATING A NEW BUFFER 
        StringBuffer s2 = new StringBuffer();  
           // CHECKING EACH CHARACTER OF CHAR. ARRAY FOR WHITE SPACE 
        for (int x= 0; x < s1.length; x++)
            if ((s1[x] != ' ') && (s1[x] != '\t'))
            // APPENDING ONLY CHARACTER TO BUFFER
                s2.append(s1[x]);  
        // CONVERTING BUFFER TO STING 
        String str = s2.toString(); 
        // PRINTING THE WHITE SPACE ELEMINATED STRING 
        System.out.println("After Eliminating Space Sting Is : " + str);  
    }  
}  
Output:

Enter a string : BTech Geeks
After Eliminating Space Sting Is : BTechGeeks

Method 2  : Java program to remove white spaces using replaceAll() method

We can remove all white space in a string by using  a built-in method called replaceAll() .

 Approach :

  • Enter a string .
  • Use built in method replaceAll() to remove all the white space and store it to a new string .
  • Print the newly created string.

Program :

import java.util.Scanner;
public class Main {  
    public static void main(String[] args)
    {  
        // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter a string : ");  
        String s= sc.nextLine();   
        // USING OF PRE-DEFINED METHOD
        // WHITE SPACES REMOVED USING REGEX
        String str = s.replaceAll("\\s+", ""); 
        // PRINTING THE WHITE SPACE ELEMINATED STRING 
        System.out.println("After Eliminating Space Sting Is : " + str);  
    }  
}  
Output:

Enter a string : BTech Geeks 
After Eliminating Space Sting Is : BTechGeeks

Method 3 : Java program to remove white spaces using replace() method

We can remove all white space in a string by using  a built-in method called replace() .

 Approach :

  • Enter a string .
  • Use built in method replace() to remove all the white space and store it to a new string .
  • Print the newly created string

 Program :

import java.util.Scanner;
public class Main {  
    public static void main(String[] args)
    {  
        // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter a string : ");  
        String s= sc.nextLine();   
        // USING OF PRE-DEFINED BUILTIN METHOD
        String str = s.replace (" ", ""); 
        // PRINTING THE WHITE SPACE ELEMINATED STRING 
        System.out.println("After Eliminating Space Sting Is : " + str);  
    }  
}  
Output:

Enter a string : BTech Geeks 
After Eliminating Space Sting Is : 
BTechGeeks

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs: