By Editorial Staff. Updated on
In this program, we are going to share a Java program to remove spaces from the string with the output. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java program to remove spaces from the string.
To increase your Java knowledge, practice all Java programs:
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
public class JavaProgramExample
public static void main(String args)
String str, strWithoutSpace;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Sentence : ");
str = scan.nextLine();
strWithoutSpace = str.replaceAll(" ", "");
char strArray = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (i = 0; i < strArray.length; i++)
if( (strArray[i] != ' ') && (strArray[i] != '\t') )
Sharing is caring!