Java Program to Remove Spaces from String

This article covers multiple programs in Java that removes spaces from a string, entered by user at run-time of the program. Here are the list of programs covered in this article:

  • Remove all spaces from string in Java
  • Remove only extra spaces from string in Java
  • Remove all spaces from string in Java without using inbuilt function
  • Remove spaces from beginning and end of the string in Java

Remove all White Spaces from String in Java

The question is, write a Java program to remove all spaces from a given string. The program given below is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String str;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      str = scan.nextLine();
      
      str = str.replaceAll(" ", "");
      
      System.out.println("\nString without Spaces = " +str);
   }
}

The snapshot given below shows the sample run of above program, with user input codescracker . com as string to remove its all spaces.

java remove all spaces from string

Note - The replaceAll() method replaces the first argument's value with the second.

Remove Extra Spaces from String in Java

Sometime we need to remove only extra white spaces from the string. Since previous program removes all whitespaces from a given string. Therefore I've created another program that removes only extra spaces. Here is the program.

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      String str = scan.nextLine();
      
      str = str.replaceAll("\\s+", " ");
      
      System.out.println("\nString without extra Spaces = " +str);
   }
}

Here is its sample run with a string, containing multiple spaces in between two words:

java remove extra spaces from string

Note - The \\s+ is a regular expression code, refers to multiple spaces. And the code \\s refers to single space. Therefore in the first program, we can also use "\\s", in place of " ".

Remove all Spaces from String without using inbuilt Function

The question is, write a program in Java, to remove all spaces from a string without using inbuilt function. The string must be received by user at run-time. The program given below is the answer to this question. This program does not uses replaceAll() method.

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String str, newstr="";
      char ch;
      int len, i;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      str = scan.nextLine();
      
      len = str.length();
      for(i=0; i<len; i++)
      {
         ch = str.charAt(i);
         if(ch!=' ')
            newstr = newstr + ch;
      }
      
      str = newstr;
      System.out.println("\nString without Spaces = " +str);
   }
}

Remove Spaces from Beginning and End of String in Java

This is the last program of this article, created to remove only leading and trailing spaces from a given string. The leading spaces are spaces available at beginning of the string. Whereas the trailing spaces are spaces available at end of the string.

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, index=0;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the String: ");
      String str = scan.nextLine();
      
      int len = str.length();
      for(i=0; i<len; i++)
      {
         if(str.charAt(i)==' ')
            continue;
         else
         {
            index = i;
            break;
         }
      }
      String newstr="";
      for(i=index; i<len; i++)
         newstr += str.charAt(i);
      
      int lenTemp = newstr.length();
      for(i=(lenTemp-1); i>=0; i--)
      {
         if(newstr.charAt(i)==' ')
            continue;
         else
         {
            index = i;
            break;
         }
      }
      String restr = "";
      for(i=0; i<=index; i++)
         restr += newstr.charAt(i);
      
      str = restr;
      System.out.println("\nString without Leading and Trailing Space(s) = " +str);
   }
}

The sample run of above program with a string that contains leading and trailing spaces, is shown in the snapshot given below:

java remove beginning ending spaces from string

I know, whether the trailing spaces are removed or not, are not being seen using above output. To see, or to cross-check the program for trailing spaces, then we need to add something after the string. That is, replace the following statement from above program:

str = restr;
System.out.println("\nString without Leading and Trailing Space(s) = " +str);

with statements given below:

System.out.println("\n---Original String---");
System.out.println("---\"" +str+ "\"---");
str = restr;
System.out.println("\n---String without Leading and Trailing Space(s)---");
System.out.println("---\"" +str+ "\"---");

Now the output with a string input that contains leading and trailing spaces, would looks like:

java remove leading and trailing spaces from string

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋