Java program to Remove spaces from a string

Remove spaces from String

In this page we will look at how to write a java program to remove spaces from a string. 

Here we will store the string in a character array lets say s and that original string will contain the spaces in between.

Example:- 

  • Input string: “Prep  insta”
  • Output string: “Prepinsta”
Java program to Remove spaces from a string

Algorithm

  • Take string input from user and store it in the variable called “s”.
  • After that convert that string to char array using toCharArray() method.
  • Make a string buffer object and run the for loop start from i=0 to i<c.length.
  • check if( (c[i] != ‘ ‘) && (c[i]!= ‘\t’ )) append that character to string buffer and after that simply print string buffer.

Code in Java (Method-1 without using in-built method)

Run
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
	  Scanner sc =new Scanner(System.in);
	  String s = "Prepinsta is best";
	  char[] c = s.toCharArray();
	  StringBuffer sb = new StringBuffer();
	  
	  
	  for (int i = 0; i < c.length; i++) {
	     if( (c[i] != ' ') && (c[i]!= '\t' )) {
	    	 sb.append(c[i]);
	     }    	
          }
	  System.out.println("String after removing spaces : "+sb);
    }
}

Output

String after removing spaces : Prepinstaisbest

Code in Java (Method-2 using in-built method)

Run
import java.util.Scanner;

public class Main {
      public static void main(String[] args) {
      StringBuffer sb = new StringBuffer();
	String s = "Prepinsta is best";
	String[] s1 = s.split("[\\s]");
	for (String string : s1) {
		sb.append(string);
	}
    System.out.println(sb);
   }
}

Output

String after removing spaces : Prepinstaisbest

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

22 comments on “Java program to Remove spaces from a string”


  • Shekhar Suman

    public static void main(String[] args) {
    String str=”Prep ins t a “;
    str=str.replaceAll(” “,””);
    System.out.println(str);
    }


  • Khushi Saxena

    import java.util.*;
    public class Main {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String s1 = “”;
    s1 = str.replaceAll(“[\\s]”,””);
    System.out.println(s1);
    }
    }
    Correct Answer!


  • Vivek

    import java.util.Scanner;
    public class RemSpace {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    s = s.replaceAll(” “,””);
    System.out.println(s);
    }
    }


  • Siddhesh

    import java.util.Scanner;

    public class Space {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the string:”);
    String s = sc.nextLine();
    char c[] = s.toCharArray();
    String s1 = ” “;
    for (int i = 0; i < c.length; i++) {
    if (c[i] != ' ') {
    s1 = s1 + c[i];
    }
    }
    System.out.println(s1);
    }
    }


  • Bishal

    class HelloWorld {
    public static void main(String[] args) {
    String str=”Prepinsta is best”;
    //char c=str.toCharArray();
    String s=””;
    for(int i=0;i<str.length();i++){
    if(str.charAt(i)!= ' ')
    s=s+str.charAt(i);
    }
    System.out.println(s);
    }
    }


  • Ziaul

    public static void removespaces(String str) {
    char ch[]=str.toCharArray();
    String rem=””;
    for(int i=0;i<str.length();i++) {
    if(ch[i]!=' ') {
    rem=rem+ch[i];
    }
    }
    System.out.println(rem);
    }


  • Buvanesh

    public class Main
    {
    public static void main(String[] args) {
    String s = “Prepinsta is best”;
    char c[] = s.toCharArray();
    for(int i=0;i<s.length();i++)
    {
    if(c[i]!=' ')
    {
    System.out.print(c[i]);
    }
    }

    }
    }


  • Vivek

    static StringBuilder removespace2(StringBuilder str){
    for(int i = 0 ; i<str.length() ; i++){
    if(str.charAt(i)==' ' ){
    str.delete(i,i+1);
    }
    }
    return str;
    }

    public static void main(String[] args) {
    String str;
    System.out.println("Enter String : ");
    Scanner sc = new Scanner(System.in);
    StringBuilder sb = new StringBuilder(sc.nextLine());
    System.out.println(removespace2(sb));
    }


  • Dipika

    public class Main {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String s1 = “”;
    s1 = str.replaceAll(” “, “”);
    System.out.println(s1);
    }
    }