Java Program to Remove All Whitespaces from a String

Java Program to Remove ALL Whitespaces from a string 

What is string in java? 

In Java, a string is an object that represents a collection of characters. Because String objects in Java are immutable, their values cannot be modified once they have been created. Java has a number of ways to work with and modify Strings, including ways to concatenate, compare, search for, and extract substrings.

  • Java provides the StringBuilder and StringBuffer classes for building Strings efficiently and dynamically.

To know more about Java Program to Remove ALL Whitespaces from a string read the complete article.

Steps to Java Program to Remove ALL Whitespaces from a string

    1. Create a string variable with the original string and all of the whitespaces.
    2. To eliminate all whitespace from the string, use the replaceAll() method from the String class. The procedure accepts a regular expression as an argument, where s matches any character that is a whitespace (space, tab, newline, etc.).
    3. Assign the returned value from the replaceAll() method to a new string variable.
    4. Print the new string without any whitespaces.

Let’s look at a Java Program to Remove ALL Whitespaces from a string is used to perform an operation.

Example 1: Java Program to Remove All Whitespaces

Run
public class Main{
  public static void main(String[] args) {
    String str = "PrepInsta is a ed-tech company";
    str = str.replaceAll("\\s", "");
    System.out.println("String after removing whitespaces: " + str);
  }
}

Output

String after removing whitespaces: PrepInstaisaed-techcompany

Example 2 : Java program to Take string from users and remove the white space

Run
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String inputString = sc.nextLine();

    String withoutWhiteSpaces = inputString.replaceAll("\\s", "");

    System.out.println("String without white spaces: " + withoutWhiteSpaces);
  }
}

Output

Enter a string: PrepInsta and  prepinsta prime
String without white spaces: PrepInstaandprepinstaprime

Example 3: Java Program to Remove All Whitespaces

Run
public class Main {

    public static void main(String[] args) {
        String sentence = "W      h  at i s p re   p In    s t a ";
        System.out.println("Original sentence: " + sentence);

        sentence = sentence.replaceAll("\\s", "");
        System.out.println("After replacement: " + sentence);
    }
}

Output

WhatisprepInsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription