Remove All White Spaces From a String
Build a new string with every whitespace character removed.
Walk the string and append each character to a builder only when it is not whitespace. Use Character.isWhitespace to catch spaces, tabs and newlines in one check. Every visible character is kept in order, so the result is the input with all gaps closed. This runs in O(n) time.
Problem Statement
Given a string, remove every whitespace character — spaces, tabs, newlines and similar — and return the remaining characters in their original order. All visible characters, including letters, digits and punctuation, are kept.
For example, Java Programming (with extra spaces) becomes JavaProgramming.
Input: A single string s.
Output: The string with all whitespace characters removed.
Examples
Input: "a b c"
Output: abcThe two spaces between the letters are removed.
Input: " Java Programming "
Output: JavaProgrammingLeading, trailing and internal spaces are all stripped.
Constraints
0 <= s.length <= 10^5Remove all whitespace, including tabs and newlines
Think Before You Code
Reveal the questions to ask yourself first
- What counts as whitespace beyond the plain space character?
- How do you test a character for whitespace with one call?
- How do you keep every non-whitespace character in order?
- How do you build the result without creating many temporary strings?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Filter out whitespace in one pass.
- Create an empty
StringBuilder. - For each character
cin the string:- If
Character.isWhitespace(c)is true, skip it. - Otherwise append
cto the builder.
- If
- Return the builder's contents.
Using Character.isWhitespace handles every whitespace form uniformly, so tabs
and newlines are removed just like ordinary spaces.
Dry Run
Walk through the example step by step
Removing whitespace from "a b c":
char | whitespace? | result so far
-----+-------------+--------------
a | no | a
(sp) | yes | a
b | no | ab
(sp) | yes | ab
c | no | abc
final: "abc"
Solution
Reveal the full Java solution
public class RemoveWhiteSpaces {
public static String removeSpaces(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!Character.isWhitespace(c)) {
sb.append(c);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(removeSpaces("a b c")); // abc
System.out.println(removeSpaces(" Java Programming ")); // JavaProgramming
}
}
Each character is checked once with Character.isWhitespace, which recognizes the
space, tab, newline and other Unicode whitespace forms. Non-whitespace characters
are appended in order, so the result is simply the input with all gaps closed. The
StringBuilder keeps the appends efficient. This removes internal spaces too,
unlike trim(), which only strips the ends.
O(n) where n is the string lengthSpace: O(n) for the output stringCommon Mistakes
- Only removing the ' ' character and leaving tabs or newlines behind.
- Using trim(), which strips only leading and trailing whitespace.
- Building the result with String concatenation in a loop.
Edge Cases to Test
- An empty string returns an empty string.
- A string of only spaces returns an empty string.
- A string with no whitespace is returned unchanged.
Interview Follow-Ups
- How would you collapse multiple spaces into a single space instead of removing all?
- How would you remove only leading and trailing whitespace without built-in trim?
- How would you remove whitespace in place given a char array?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →