Overview
This post explains how to normalize and remove whitespace characters from strings in Java. Handling whitespace correctly is a common requirement in text processing and input validation, and Java provides multiple techniques depending on your goals — whether you want to trim spaces, remove all whitespace, or collapse multiple spaces into one.
Whitespace includes:
- Space characters
- Tabs (
\t) - Newlines (
\n,\r) - Other Unicode whitespace characters
Definitions
Whitespace normalization refers to:
- Removing unwanted whitespace at the beginning and end of a string
- Reducing multiple consecutive whitespace characters inside a string to a single space
This improves consistency in text for comparison, storage, and display.
Using String Methods to Normalize Whitespace
Trim Leading and Trailing Whitespace
Java provides built-in methods to remove whitespace at the beginning and end of a string:
String input = " Hello World ";
String trimmed = input.trim(); // Removes leading and trailing spaces (ASCII)
If using Java 11 or later, prefer the Unicode-aware strip() method:
String stripped = input.strip();
trim()removes only basic ASCII whitespacestrip()removes all Unicode whitespace characters
Remove All Whitespace
To remove every whitespace character (spaces, tabs, newlines) from a string:
String noWhitespace = input.replaceAll("\\s", "");
\\sin regex matches all whitespace characters- This produces a string with no whitespace at all
Normalize Internal Spacing
If you want to collapse multiple internal whitespace characters into a single space while also trimming ends:
String normalized = input.strip()
.replaceAll("\\s+", " ");
This sequence:
- Removes leading/trailing whitespace
- Replaces one or more contiguous whitespace characters with a single space
Example:
String input = " Hello World from Java ";
String normalized = input.strip()
.replaceAll("\\s+", " ");
// Result: "Hello World from Java"
Unicode-Aware White Space Handling
Starting with Java 11, strip(), stripLeading(), and stripTrailing() handle Unicode whitespace more comprehensively than the older trim() method.
Use these methods when dealing with international text or inputs from diverse sources.
Practical Example
public class WhitespaceExample {
public static void main(String[] args) {
String raw = " Hello World \t from Java ";
String trimmed = raw.strip(); // "Hello World \t from Java"
String noAllWhitespace = raw.replaceAll("\\s", ""); // "HelloWorldfromJava"
String normalized = raw.strip().replaceAll("\\s+", " "); // "Hello World from Java"
System.out.println("Trimmed: [" + trimmed + "]");
System.out.println("No Whitespace: [" + noAllWhitespace + "]");
System.out.println("Normalized: [" + normalized + "]");
}
}
Choosing the Right Method
| Goal | Recommended Method |
|---|---|
| Trim leading and trailing whitespace | strip() (Java 11+) |
| Remove all whitespace characters | replaceAll("\\s","") |
| Collapse internal whitespace to single spaces | strip().replaceAll("\\s+"," ") |


