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)

import java.util.Scanner;

public class RemoveAllWhiteSpacesFromAString {

	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)

import java.util.Scanner;

public class RemoveAllWhiteSpacesFromAString {
      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

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


  • A

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    String s= “you are going to rock”;
    StringBuffer s1=new StringBuffer();
    for(char i: s.toCharArray())
    {
    if(i !=’ ‘)
    s1.append(i);
    }
    System.out.println(s1);

    }

    }


  • Srinibash

    import java.util.Scanner;
    public class p8 {
    public static void main(String[] args){

    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter a String”);
    String s1 = sc .nextLine();
    for(int i = 0 ; i <s1.length();i++){
    s1=s1.replace(" ","");
    }
    System.out.println(s1);
    }

    }


  • Rohit

    for(int i=0;i<s.length();i++)
    {
    if((s.charAt(i))==' ')
    {
    i++;
    }
    System.out.print(s.charAt(i));
    }

    //remember not to use nextline//


  • Surya

    string1=input()
    string2=str()
    for i in string1:
    if (i==” “):
    pass
    else:
    string2=string2+i
    print(string2)


  • Surya

    #pyhton code
    string1=input()
    string2=str()
    for i in string1:
    if (ord(i)==32):
    pass
    else:
    string2=string2+i
    print(string2)


  • NITIN

    In python:-
    n = input()
    a =[]
    for i in n :
    if i != ‘ ‘:
    a.append(i)
    else:
    continue
    print(a)
    for i in a:
    print(i,end = ”)


  • SUSOBHAN

    import java.util.Scanner;

    public class Remove_space {
    public static void main(String[] args) {
    String Str;
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter a sentence: “);
    Str = sc.nextLine();
    String s = Str.replace(” “,””);
    System.out.println(s);

    }
    }


  • SIDDHANT

    String s=”p rep in st a”;
    StringBuffer temp=new StringBuffer();
    for(int i=0;i<s.length();i++)
    {
    if(Character.isWhitespace(s.charAt(i)))
    {
    continue;
    }
    else
    {
    temp.append(s.charAt(i));
    }
    }
    System.out.println(temp);


  • chetan

    String s1 = sc.nextLine();
    System.out.println(s1.replaceAll(“[ ]”,””));


  • Hrishi

    Easy Method:
    String s=”I love my India”;
    for (int i = 0; i < s.length(); i++) {

    if((s.charAt(i))!=' ') {
    System.out.print(s.charAt(i));