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”
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
Note
Make a StringBuffer object and check each for each character array that element should not be blank there should not be any whitespaces if the condition is true append that element to StringBuffer object after that print StringBuffer object in which String doesn't contain any blank spaces.
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
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);
}
}
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);
}
}
for(int i=0;i<s.length();i++)
{
if((s.charAt(i))==' ')
{
i++;
}
System.out.print(s.charAt(i));
}
//remember not to use nextline//
string1=input()
string2=str()
for i in string1:
if (i==” “):
pass
else:
string2=string2+i
print(string2)
#python*
#pyhton code
string1=input()
string2=str()
for i in string1:
if (ord(i)==32):
pass
else:
string2=string2+i
print(string2)
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 = ”)
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);
}
}
System.out.print(s.replaceAll(“\\s”, “”));
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);
String s1 = sc.nextLine();
System.out.println(s1.replaceAll(“[ ]”,””));
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));