Write a Java program to remove white spaces from a String using Built-In Methods?

 

package testjava.javatest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PrimeOrNot
{
	public static void main(String args[]) throws NumberFormatException, IOException
	{
		System.out.print("Enter any String: ");
   	 
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String stringWithSpace;
    	stringWithSpace = br.readLine();
    	
    	String stringWithoutSpace = stringWithSpace.replaceAll("\\s+", "");
        
        System.out.println("Input String : "+stringWithSpace);        
        System.out.println("Input String Without Spaces : "+stringWithoutSpace);       
	}
}

Output

Enter any String: This is programs buzz

Input String : This is programs buzz

Input String Without Spaces : Thisisprogramsbuzz