BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to remove all the white spaces from a string

Last Updated: July 14, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write a java program to remove all the whitespaces from a string. To do that we are using replaceAll() method and inside which we are using regex to replace all white spaces with blank.

Java Program to remove all the white spaces from a string

In this example, we have a string str and this string contain whitespaces. We are using replaceAll() method to replace the whitespaces in the existing string with the blanks. This will remove the whitespaces from the given string.

public class JavaExample {
  public static void main(String[] args) {

    String str="Hi, Welcome to BeginnersBook.com";
    System.out.println("Original String:" + str);

    //Using regex inside replaceAll() method to replace space with blank
    str = str.replaceAll("\\s+", "");

    System.out.println("String after white spaces are removed:" + str);
  }
}

Output:
Java Program to remove all the white spaces from a string

Example 2: Program to remove whitespace from user input string

This example is same as the above example except that the string here is not hardcoded and is entered by the user.

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {

    String str;
    System.out.println("Enter any String: ");
    Scanner sc = new Scanner(System.in);
    str = sc.nextLine();
    System.out.println("Original String: " + str);

    //replacing white spaces space with blank
    str = str.replaceAll("\\s+", "");

    System.out.println("String after white spaces are removed: " + str);
  }
}

Output:
Java Program to remove all the white spaces from a string

Related Java Examples:

  • Java Program to find the longest repeating sequence in a string
  • Java Program to find longest substring without repeating characters
  • Java Program to find all subsets of a string
  • Java Program to divide a string in ‘n’ equal parts
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to find longest substring without repeating characters
  3. Java Program to find all subsets of a string
  4. Java program for bubble sort in Ascending & descending order
  5. java program to find factorial of a given number using recursion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap