Whitespace Cleaner

Trim lines, collapse extra spaces, remove blank lines, and normalize whitespace — all in one click.

Input
Operations


Frequently Asked Questions

It removes all leading and trailing spaces (and tabs) from each individual line. The content between the line edges is unchanged. This is equivalent to calling trim() on each line in PHP, or .strip() in Python.

It replaces any run of two or more consecutive space characters (within a line) with a single space. It does not collapse newlines, only horizontal spaces. Use it to clean up copy-pasted text from PDFs, HTML, or word processors.

This applies three operations in sequence: (1) trim each line, (2) collapse extra spaces, and (3) collapse multiple consecutive blank lines to a single blank line. This preserves paragraph structure while removing messy whitespace — the most common cleaning task.

Whitespace encompasses more characters than just the spacebar. Key types include: Space (U+0020) — the standard space; Tab (\t, U+0009) — horizontal tab used for indentation; Newline (\n, U+000A) — line feed ending a line on Unix/macOS; Carriage Return (\r, U+000D) — used in Windows line endings (\r\n); Non-Breaking Space ( , U+00A0) — a space that prevents line wrapping; Zero-Width Space (U+200B) — invisible, zero-width, often invisible to the eye but present in the string; and Em Space / En Space (U+2003/U+2002) — typographic spacing characters from Unicode. Many whitespace bugs involve these less common variants.

Extra whitespace silently breaks many operations. In JSON parsing, whitespace inside string values is part of the string — "John " and "John" are different keys. In CSV files, a field containing " value" (leading space) will not match a lookup for "value", causing import failures. In string comparisons, "active " === "active" evaluates to false in most languages, causing authentication and lookup bugs. In HTML, multiple spaces collapse to one visually, but the extra bytes persist in the source. In Python / YAML, leading spaces have syntactic meaning — an extra space can change indentation level and break the entire file structure.

Trim removes whitespace from the start and end of a string (or each line) only — it does not affect whitespace in the middle. For example, " hello world " becomes "hello world". Normalize whitespace (also called collapsing) replaces all runs of whitespace within the string with a single space and trims the edges: " hello world " becomes "hello world". Most programming languages provide trim() or .strip() for the former. Normalization typically requires a regex like /\s+/g replaced with a single space after trimming. Use trim for input validation; use normalize when processing natural language text.

A regular space (U+0020) allows the browser to break a line at that point when text wraps. A non-breaking space ( , U+00A0) looks identical but prevents a line break — the browser treats it as glue between two words. Non-breaking spaces are used between a number and its unit ("10 kg") or between parts of a name that should not be separated across lines. The problem is that   is often copy-pasted into databases and code editors where it is mistaken for a regular space but does not match regex patterns like \s (in some implementations) or string comparisons for " ", causing subtle lookup failures.

Trailing whitespace — spaces or tabs at the end of a line — is invisible in most editors but causes problems in several areas. In git diffs, trailing spaces appear as noise in code reviews and are flagged by many style guides. Most projects configure .editorconfig or .gitattributes to strip trailing whitespace on save. In Python, trailing whitespace can cause unexpected indentation errors. Linters (ESLint, Pylint, RuboCop) flag trailing whitespace as a code style violation. In Makefiles, a tab-only line with trailing space can break build rules. Keeping code free of trailing whitespace reduces diff noise and prevents subtle bugs in whitespace-sensitive languages.

In JSON, whitespace is insignificant outside of string values — spaces, tabs, and newlines between tokens (keys, colons, commas, brackets) are ignored by parsers and used only for formatting. However, whitespace inside a string value is significant and preserved exactly. The value "John Smith" and "John Smith" (double space) are different strings. Similarly, a leading or trailing space inside a string value is part of that string. When processing JSON data from external APIs, always trim string values before using them as keys or doing lookups to avoid whitespace mismatch bugs.

Invisible characters are Unicode code points that render as blank space or nothing at all but are present in the string. Common ones include: Zero-Width Space (U+200B), Zero-Width Non-Joiner (U+200C), Zero-Width Joiner (U+200D), Word Joiner (U+2060), and BOM (U+FEFF). They often appear when copying text from web pages, PDFs, or word processors. To detect them: paste the text into this tool and look for characters that appear blank in the frequency table; use a hex editor or an online Unicode inspector; or search with the regex /[​-‍]/g in your code. The "Remove ALL whitespace" operation in this tool will strip them along with standard whitespace.

About This Whitespace Tool

This free whitespace tool provides several operations for cleaning whitespace in text: trim leading and trailing spaces, collapse multiple consecutive spaces into one, remove blank lines, and strip all whitespace. Select an operation and the result appears instantly in your browser.

When to use this tool

  • Cleaning copy-pasted text that has extra blank lines or indentation
  • Normalising whitespace before importing or processing data
  • Stripping leading and trailing spaces from a list of values
  • Collapsing multiple spaces in reformatted or OCR-processed text

Standards & References

How It Works

Paste Messy Text

Paste text with inconsistent whitespace, extra spaces, blank lines, or tabs into the input area on the left.

Click an Operation

Each button applies a specific regex-based transformation: trim, collapse spaces, remove blank lines, convert tabs, or remove all whitespace.

See & Copy Result

The cleaned text appears in the output card with a before/after character count. Click Copy to grab it. Reset restores the original.

Common Use Cases

PDF Copy-Paste Cleanup

Text copied from PDFs often has extra spaces and broken line breaks. Use Trim + Collapse to restore clean paragraphs in seconds.

Code Pasting Normalization

Convert tabs to spaces (or vice versa) when migrating code between editors with different indentation settings to prevent syntax errors.

CSV & Data Prep

Trim leading and trailing spaces from each value in a CSV export before importing to a database, preventing mismatched lookups and key errors.

Article & Blog Formatting

Remove excessive blank lines pasted from a Word document into a CMS. "Collapse multiple blank lines" preserves paragraph breaks while removing extras.

Email Template Cleanup

Strip extra whitespace from copy-pasted email content before sending. Clean whitespace prevents unexpected rendering differences across email clients.

Log File Normalization

Normalize inconsistent whitespace in multi-source log files before running regex searches or diff comparisons across log entries.

Related Articles

View all articles
Trailing Whitespace and Git Diff Noise: Why a One-Line Fix Can Show 200 Lines Changed

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.

Jun 17, 2026
Whitespace as Syntax: Python Indentation, YAML Structure, Prettier, and .editorconfig

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.

Jun 11, 2026
Invisible Unicode Characters: Security Risks, Homoglyph Attacks, and Text Watermarking

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.

Jun 9, 2026
Whitespace Cleaner — Remove Invisible Characters & Normalise Any Text

Whitespace Cleaner — Remove Invisible Characters & Normalise Any Text

Learn what causes whitespace problems in text — non-breaking spaces, zero-width characters, BOM markers, mixed line endings — and how to use a free whitespace cleaner to normalise any text instantly.

Jun 6, 2026