This post contains a total of 8+ Java Blank Space Remover Program Examples with Source Code. All these Blank Space Remover Programs are used to remove Blank / White Space from Strings, and these programs are made using Java.
You can use the source code of these examples with credits to the original owner.
Related Posts
Java Blank Space Remover Programs
1. By Hatsy Rei
Made by Hatsy Rei. A Very basic java program to remove blank space from a string. Source
hel lo hello
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
System.out.print(new Scanner(System.in).nextLine().replace(" ", ""));
}
}
2. By Baraa AB
Made by Baraa AB. Program with two methods to Remove Spaces from a string. Source
Enter a Sentence : we re were
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
String str, strWithoutSpace;
int i;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Sentence : ");
str = scan.nextLine();
//1. Using replaceAll() Method
strWithoutSpace = str.replaceAll(" ", "");
System.out.println(str);
//2. Without Using replaceAll() Method
char[] strArray = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (i = 0; i < strArray.length; i++)
{
if( (strArray[i] != ' ') && (strArray[i] != '\t') )
{
sb.append(strArray[i]);
}
}
System.out.println(sb);
}
}
3. By Aya
Made by Aya. Simple Empty space remover program. Source
a b c abc
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String srt=input.nextLine();
srt=srt.replaceAll("\\s","");
System.out.print(srt);
}
}
4. By Muhd Khairul Amirin
Made by Muhd Khairul Amirin. Source
************** *Remove Spaces ************** 1 2 3 123
public class RemoveSpace
{
static void print(String a){
System.out.println(a);
}
public static void main(String[] args) {
print(" **************");
print(" *Remove Spaces");
print(" **************");
System.out.println(new java.util.Scanner(System.in).nextLine().replaceAll(" ",""));
}
}
5. By GTT
Made by GTT. Simple Java Program to remove Empty space from text and number strings. Source
Enter a string:: de mo demo
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string:: ");
String input = scan.nextLine();
//edit1 Display original string
System.out.println(input);
//split input to char array
char[] characters=input.toCharArray();
//create new copy reference
String newCopy = "";
//use enhanced for to loop &
//"remove" any space characters
for(char c : characters) {
if(c != ' ') {
newCopy += c;
}
}
//Is the result as expected?
System.out.println(newCopy);
}
}
6. By Deepraj Chawda
Made by Deepraj Chawda. Source
enter String qw er ty qwerty
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
String str;
System.out.println("enter String ");
str =sc.nextLine();
String s=str.replace(" ","");
System.out.println(s);
}
}
7. By Brianis Ruiz
Made by Brianis Ruiz. Program to remove blank space and special characters from a string. Source
[email protected] 1 11
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
String p;
p=leer.nextLine();
System.out.println(p.replaceAll(" ","").replaceAll ("[^a-zA-Z0-9\\s]+",""));
}
}
8. By Kalaiselvan
Made by Kalaiselvan. Enter your own string in “String s” to get the output string without spaces. Source
String with space:=demo text String without space:=demotext
public class Program
{
public static void main(String[] args) {
String s= "ab c d e fgh i j kl mn opqr stuvwxyz";
StringBuilder build=new StringBuilder();
for(int i=0;i<s.length();i++){
if(s.charAt(i) !=' '){
build.append (s.charAt(i));
}
}
String ss=build.toString();
System.out.println("String with space:="+s);
System.out.println("String without space:="+ss);
}
}
9. By Saroj Patel
Made by Saroj Patel. Source
Input : 1 2 3 Output : 123
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
System.out.println("Input : "+input);
System.out.println("Output : "+input.replace(" ",""));
}
}