Whitespace Cleaner

Trim, collapse, and clean whitespace from any text. Remove leading/trailing spaces, collapse multiple spaces into one, remove blank lines, convert tabs, and more.

Input Text
Operations

Frequently Asked Questions

When copying from PDFs, word processors, HTML pages, or email clients, the clipboard often includes hidden characters: multiple spaces where layout was used for alignment, hard line breaks from fixed-width columns, non-breaking spaces ( ), zero-width spaces, and tab characters used as column separators.
A regular space (U+0020) allows word-wrapping and is collapsed by HTML. A non-breaking space (U+00A0,  ) prevents line breaks at that position and is not collapsed. Non-breaking spaces often cause display issues when pasted into text fields because they look like spaces but behave differently in code and databases.
Trim means removing leading and trailing whitespace from a string. Most languages have a built-in: PHP: trim($str); JavaScript: str.trim(); Python: str.strip(); Java: str.trim(). ltrim/rtrim trim only one end.
To normalise whitespace: str.trim().replace(/\s+/g, ' ') — trims both ends and collapses multiple spaces. For lines: str.split('\n').map(l => l.trim()).filter(l => l).join('\n'). To remove ALL whitespace: str.replace(/\s/g, '').
PDFs store text as positioned glyphs, not flowing paragraphs. When text is extracted, lines from column layouts and headers often appear as separate paragraphs with blank lines between them. The "Remove blank lines" and "Collapse blank lines" operations fix the most common cases.
In HTML, consecutive whitespace is normally collapsed to a single space by the browser renderer. This is why HTML source code can include readable line breaks and indentation without affecting visual output. The white-space: pre CSS property disables collapsing.
LF (\n): Unix/Linux/macOS. CRLF (\r\n): Windows. CR (\r): Old Mac. When pasting Windows text into Unix tools, visible ^M characters appear. Use "Trim lines" to remove stray CR characters.
A tab is a single character whose display width is configurable (usually 4 or 8 spaces). Mixing tabs and spaces causes alignment issues because tab width varies between tools. Many style guides (Python PEP 8, Prettier) standardise on spaces. "Tabs → 4 spaces" converts tabs to a fixed-width equivalent.
Trailing spaces in database fields cause comparison failures: "John " !== "John". Leading spaces break sorting and search. Extra blank lines in CSV, JSON, or YAML can cause parse errors. Inconsistent newline characters cause diffs to show entire files as changed.
A zero-width space (U+200B) is an invisible character that allows word breaking but takes no visual space. It appears in text copied from web pages and some word processors. Use Find & Replace with the literal character to remove it — it is invisible in editors but causes problems in data fields, URLs, and comparisons.

About This Whitespace Cleaner

This free whitespace cleaner provides targeted operations for trimming, collapsing, and removing whitespace from any text. Each operation targets a specific whitespace problem, giving you precise control over text normalisation.

When to use this tool

  • Cleaning pasted text from PDFs, Word, or HTML sources
  • Normalising whitespace before inserting text into databases
  • Preparing text for comparison or search operations
  • Converting tab-indented code to space-indented

Standards & References

Related Articles

In-depth guides and technical articles.

View all →
AI-Generated Content Has a Whitespace Fingerprint — Why Normalisation Matters Before Publishing or Processing
AI-generated content carries whitespace patterns — double-line-spacing from training data, inconsistent bullet markers, occasional zero-width Unicode characters — that cause rendering issues when pasted into CMSs. Here's how to normalise whitespace before LLM input (reducing tokens and improving output), why whitespace sensitivity in text diff changes what differences are shown, and the specific Word document whitespace artifacts (non-breaking hyphens, soft hyphens, figure spaces) that appear on export.
Why Copy-Paste From PDFs and Word Docs Silently Breaks Your Database: The Invisible Character Problem
A non-breaking space (U+00A0) looks identical to a regular space but doesn't trim with standard .trim(), doesn't match in string comparisons, and passes "not empty" validation on fields that look blank to users. Here's the taxonomy of invisible Unicode characters that cause real data quality bugs (not security exploits — the accidental kind from PDF and Word paste), where they come from, the specific bugs they cause, and how to reliably detect and clean them.
Trailing Whitespace and Git Diff Noise: Why a One-Line Fix Can Show 200 Lines Changed
A one-line bug fix can show up as a 200-line diff if an editor's "trim trailing whitespace on save" setting touches every line in a file that previously had inconsistent trailing whitespace. Here's why this happens, git's whitespace-ignoring diff flags, dedicated "cleanup commit" practices for git blame hygiene, and the Markdown exception where trailing whitespace is actually meaningful.
Whitespace as Syntax: Python Indentation, YAML Structure, Prettier, and .editorconfig
Python uses indentation as syntax; YAML prohibits tabs entirely; Make requires tabs. Here's whitespace as code structure, the tabs vs spaces debate resolved by autoformatters like Prettier and Black, .editorconfig for cross-editor consistency, and the CRLF vs LF line ending problem in mixed-platform teams.
Invisible Unicode Characters: Security Risks, Homoglyph Attacks, and Text Watermarking
Zero-width characters, BiDi control codes, and homoglyphs are used in phishing attacks, document watermarking, and the Trojan Source code injection vulnerability. Here's what invisible Unicode characters are, why they cause bugs, and how to detect and remove them.