Remove whitespaces from a string in Java



 Write a Java program to remove whitespaces from a string.

Java program to Remove whitespaces from a string

public class Main {

    public static void main(String[] args) {

    	String str = " Java is my fav programming language. I love Java coding";
    	
        System.out.println("Input text: \n" + str + "\n");

        System.out.println("replaceAll() solution:");

        String result = str.replaceAll("\\s", "");

        System.out.println("String without blanks is: \n" + result);
    }
}

Output:

Input text: 
 Java is my fav programming language. I love Java coding

replaceAll() solution:
String without blanks is: 
Javaismyfavprogramminglanguage.IloveJavacoding

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments