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.
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
This does not remove tab or newline characters.
Remove all whitespace with regex
r"\s+" matches one or more whitespace characters, including line breaks.
Fast split and join pattern
This pattern is concise and often fast for large inputs.
JavaScript Approaches
In JavaScript, regex replacement is the most common method.
If you only want to remove normal spaces:
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.
For file processing:
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:
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
- Remove all whitespace in a string
- Remove all whitespace in a string
- Remove characters after specific character in string, then remove substring?
- Remove final character from string
- Remove Last Two Characters in a String
- Remove Last Two Characters in a String
- Remove text in-between delimiters in a string using a regex?
- Remove the last three characters from a string
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.