Kodekraftt
/Blog
/Remove All Whitespaces From A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Remove All Whitespaces from a String in Java — Definition, Examples and Program

Learn how to remove all whitespaces from a string in Java using a simple character-based approach without relying on regular expressions or built-in replace methods.

Remove All Whitespaces from a String in Java

Removing whitespaces from a string means eliminating all space characters (including spaces, tabs, and new lines) and keeping only the actual content.

This operation is commonly used in input validation, data cleaning, and preprocessing user-entered text.

How the Logic Works

  1. Read the input string.
  2. Convert the string into a character array.
  3. Traverse each character one by one.
  4. If the character is not a whitespace, add it to the result.

Example 1

Input: "Java Programming"

Output: JavaProgramming

Example 2

Input: " Learn Java Fast "

Output: LearnJavaFast

Java Program to Remove All Whitespaces


import java.util.Scanner;

public class RemoveWhitespaces {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        String result = "";

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

            if (ch != ' ' && ch != '\t' && ch != '\n') {
                result = result + ch;
            }
        }

        System.out.println("String without whitespaces: " + result);
    }
}

Sample Output


Enter a string: Java   is   awesome
String without whitespaces: Javaisawesome

Important Notes

  • All types of whitespace characters are removed.
  • No replace() or regular expressions are used.
  • Demonstrates character-level string processing.

Practice Challenges

  1. Modify the program to remove only extra spaces between words.
  2. Count how many whitespaces were removed.
  3. Rewrite the program using StringBuilder for better performance.

This program is a great exercise to understand how strings work internally and how to process user input efficiently in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.