Text Editing
White Spaces
Coding
Text Processing
Programming Techniques

Remove ALL white spaces from text

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Removing all whitespace from text is common in preprocessing, normalization, and identifier generation. The tricky part is deciding what counts as whitespace and whether you want to remove only ASCII spaces or every Unicode whitespace character. A correct solution starts with a clear rule, then applies language-specific tools that enforce that rule consistently.

Define the Whitespace Policy First

Before writing code, decide whether you want to remove:

  • only the normal space character.
  • spaces, tabs, and newlines.
  • all Unicode whitespace characters recognized by regex engines.

These are not equivalent. A pipeline that removes only regular spaces can leave tabs or non-breaking spaces, which can break deduplication, hashing, or comparisons.

For most data cleanup tasks, removing all regex whitespace is the safest default.

Python Approaches

Python offers several options depending on readability and speed needs.

Remove only regular spaces

python
text = "A  B\tC\nD"
clean = text.replace(" ", "")
print(clean)  # A\tBC\nD

This does not remove tab or newline characters.

Remove all whitespace with regex

python
1import re
2
3text = "A  B\tC\nD"
4clean = re.sub(r"\s+", "", text)
5print(clean)  # ABCD

r"\s+" matches one or more whitespace characters, including line breaks.

Fast split and join pattern

python
text = "A  B\tC\nD"
clean = "".join(text.split())
print(clean)  # ABCD

This pattern is concise and often fast for large inputs.

JavaScript Approaches

In JavaScript, regex replacement is the most common method.

javascript
const text = "A  B\tC\nD";
const clean = text.replace(/\s+/g, "");
console.log(clean); // ABCD

If you only want to remove normal spaces:

javascript
const text = "A  B\tC\nD";
const clean = text.replace(/ /g, "");
console.log(clean); // AB\tC\nD

The global flag g is required when all matches should be replaced, not only the first one.

Command Line and Data Pipelines

In shell workflows, you can normalize text quickly without opening an IDE.

bash
echo -e 'A  B\tC\nD' | tr -d '[:space:]'

For file processing:

bash
tr -d '[:space:]' < input.txt > output.txt

Use this carefully when whitespace can be meaningful, such as in natural language content or structured text formats that rely on spacing.

Practical Use Cases

  • Creating canonical keys for matching user input.
  • Normalizing phone numbers and codes before storage.
  • Preprocessing compact machine-readable tokens.
  • Cleaning inconsistent copy-pasted values from CSV exports.

For user-visible text, removing all whitespace can damage readability. In those cases, collapsing repeated spaces to a single space is often better than total removal.

Validation and Safety Checks

When whitespace removal is part of a data pipeline, add tests that include:

  • tabs and newlines.
  • multiple consecutive spaces.
  • non-breaking spaces from web content.
  • already-clean strings.

Example Python test cases:

python
1def strip_all_ws(s: str) -> str:
2    return "".join(s.split())
3
4assert strip_all_ws("A B") == "AB"
5assert strip_all_ws("A\tB\nC") == "ABC"
6assert strip_all_ws("  ") == ""
7assert strip_all_ws("") == ""

These checks catch subtle breakage when regex behavior or preprocessing order changes.

Common Pitfalls

  • Removing only regular spaces when tabs and newlines are also present.
  • Forgetting global replacement in JavaScript regex operations.
  • Applying aggressive whitespace removal to user-facing text and harming readability.
  • Assuming all environments treat Unicode whitespace identically.
  • Running cleanup after hashing or comparison instead of before, causing mismatch bugs.

Summary

  • Define your whitespace policy before implementation.
  • Use regex whitespace removal when you need broad coverage.
  • Prefer tested utility functions instead of ad hoc inline replacements.
  • Treat command-line cleanup as powerful but potentially destructive.
  • Validate with mixed whitespace inputs to keep preprocessing reliable.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.