Home >>Java Programs >Java Program to remove all the white spaces from a string
In this example, we will create a java program to remove all the white-spaces from the string.
To do this, we will traverse the string and check if any character of the string is matched with a white-space character or not. If so then we will use a built-in method like replace() with a blank.
public class Main
{
public static void main(String[] args)
{
String str1="Hello everyone.. Welcome Here.";
str1 = str1.replaceAll("\\s+", "");
System.out.println("String after removing all the white spaces : " + str1);
}
}