Remove Extra Spaces from Text — Collapse Whitespace Online
Clean up double spaces, tabs, and leading or trailing whitespace from any text. Choose between collapsing runs, deleting all spaces, or trimming each line — results appear instantly.
Characters: 0
Characters: 0
Why extra spaces end up in text
PDF export splits each typeset line at the margin and inserts a line break; many tools then join those lines with a space, producing double spaces between words wherever a sentence crossed a line boundary. Microsoft Word sometimes adds an extra space after a period when autocorrect fires. HTML source that gets pasted into a plain-text field carries non-breaking spaces (U+00A0) that look like ordinary spaces but do not behave like them.
Manual entry is just as common a source. Pressing the spacebar twice after a period is a habit from typewriter days that many people carry forward. Exported CSV and TSV files often pad field values with spaces to align columns visually. Spreadsheet formulas that concatenate strings frequently leave an accidental leading or trailing space on the result. Any of these can cause subtle failures downstream.
How to collapse or remove extra spaces
Paste your text into the box. The character counter on the right shows the input length immediately, before any processing takes place.
Choose a mode. Collapse multiple spaces reduces any run of two or more consecutive space characters (U+0020) to a single space — this is the right choice for prose. Delete all spaces removes every space character entirely, which is what you want when working with machine-readable tokens or identifiers. Trim each line strips leading and trailing spaces from every line independently, leaving anything in the middle of a line unchanged. Trim document removes only the very first and last characters if they are spaces.
Press the button, check the before/after character count, then copy. If the counts look wrong, compare the two boxes directly before committing to the result.
A concrete example
Input: "Hello world. How are you? " — 31 characters including the trailing space. Collapse mode produces "Hello world. How are you?" — 25 characters, a reduction of 6. No words were changed, no punctuation was touched.
Input: " line 1 " followed by a newline and " line 2 " — 22 characters. Trim each line produces "line 1" and "line 2" on separate lines — 13 characters. The line structure is preserved; only the edge padding is gone.
Input for a data task: "SKU 12345" in a product field. Delete all spaces produces "SKU12345". Collapse produces "SKU 12345". The right choice depends on whether the space was ever meaningful.
Where extra spaces cause real problems
Database queries: a name column that stores "Alice " (trailing space) will not match "Alice" in a WHERE clause without a TRIM in the query. This is one of the most common sources of "row not found" bugs in applications that accept user-entered text.
Search and autocomplete: most search libraries perform exact or prefix matching. A leading space before a word prevents the word from being found when a user types without the space. A trailing space on a query string breaks autocomplete suggestions.
JavaScript string splitting: "a b".split(" ") produces ["a", "", "b"] — an empty string in the middle — rather than the expected two-element array. Functions that assume clean single spaces break silently.
Word-processing exports: a document that looks correct in print can fail validation in a CMS that enforces a character limit on a headline field, because the character counter includes the invisible trailing space.
Email personalisation: a first name field containing "Alice " produces the greeting "Hello Alice ," when the template appends punctuation directly after the merge tag.
Four modes compared
Collapse consecutive spaces is the default and the right choice for prose. It turns "was quick" into "was quick" without touching any other character. Character count typically drops by five to twenty percent on PDF-sourced text, and drops very little on manually typed text.
Delete all spaces suits identifiers, codes, and tokens. "A B C" becomes "ABC". This is irreversible in the sense that you cannot tell afterwards where the spaces were, so check the output before using it anywhere.
Trim each line suits formatted data. A CSV field that reads " 12345 " becomes "12345". Lines that consist only of spaces become empty lines rather than disappearing — if you want those gone too, pass the result through the remove duplicate lines tool with blank-line deduplication enabled.
Trim document removes one leading and one trailing space from the whole text block, not per line. This is useful when a paste operation added a single stray space at the very start or end.
Four common mistakes with space removal
Using delete-all on prose. Every sentence becomes one unbroken string of characters. Delete-all is correct for token-shaped data, not for human-readable text.
Not checking for words already joined. If the source had a line break with no space before it, two words are already glued together before this tool runs. Collapsing extra spaces will not split them. A careful read of the output is the only reliable check.
Forgetting tabs. This tool operates on space characters (U+0020). Tab characters (U+0009) are distinct and are not processed here. If your text came from a terminal, a spreadsheet, or a code editor, tabs may be the actual problem — the invisible character remover handles tab normalisation alongside other whitespace variants.
Running collapse on indented code. A code block that uses spaces for alignment will have its indentation destroyed if you collapse spaces across the whole text. Limit processing to the prose sections, or use trim-per-line which only affects edge spaces and leaves indentation inside a line intact.
Your text stays in your browser
Processing runs as JavaScript inside the page tab. Nothing is sent to any server, nothing is written to your device, and closing the tab clears everything. You can verify this by watching the browser network panel while pressing the button — no request leaves the page.
This matters for confidential content: client emails, internal documents, contract text, or any text you would not want passing through an unknown server. The tool sees the characters only; it does not log them or measure them for any purpose.
When to use a different tool
If your text has invisible Unicode characters — zero-width spaces, non-breaking spaces (U+00A0), or bidirectional marks — the invisible character remover identifies and removes them with a per-character report. Non-breaking spaces look identical to ordinary spaces but are technically different characters and will not be caught by this tool's collapse mode.
If unwanted line breaks rather than spaces are the main issue, use the remove line breaks tool, which replaces or deletes newline characters without touching spaces.
If the result of trimming has repeated lines, pass it through the remove duplicate lines tool. If symbols and punctuation need to go as well, the remove special characters tool handles that step.
Where extra spaces actually come from
Copying from a PDF is the most common culprit. PDF layout engines break each typeset line at the right margin and store the text as a sequence of short fragments. When you select and copy, your OS joins those fragments with a space — so a sentence that crossed a line boundary inside the PDF arrives in your clipboard with a double space at the join point. A 10-page report can easily introduce 200+ double spaces this way.
Microsoft Word adds its own surprises. The default autocorrect setting in older Word versions inserts two spaces after a sentence-ending period when it detects you typed only one. The document looks fine on screen, but once you export to plain text or paste into a web form you get systematic double spaces after every full stop. Word also lets users indent paragraphs with tab characters mixed with spaces — three tabs plus two spaces in front of a line produces a leading whitespace run that collapse mode cannot fully resolve because the tabs are different code points.
Web copy-paste brings non-breaking spaces. HTML pages use (U+00A0) to prevent line breaks in specific spots — between a number and its unit, between a first name and last name, inside navigation labels. When you copy that text, U+00A0 characters ride along. They look exactly like regular spaces in any text editor, but most string operations treat them differently: a regex \s may or may not match them depending on the engine, and SQL TRIM skips them. You end up with spacing that looks fixed but is not.
Code blocks and indentation use spaces that are intentional. A Python file indented with four-space blocks is supposed to have runs of four spaces at the start of each line. Running collapse mode on the whole file would destroy that indentation. The same applies to Markdown: a two-space indent before a nested list item is part of the syntax — remove it and the nesting breaks. Always scope space cleanup to the prose sections of mixed documents, not the whole file.
Chinese and Japanese full-width spaces (U+3000, the ideographic space) look like a wide blank on screen. Copy-pasting East Asian text that used U+3000 for indentation or alignment produces a character that is not caught by any of the four modes here, because U+3000 is not U+0020. If you see spacing that persists after collapsing, that is usually the cause. Run it through the invisible character remover to map U+3000 to a standard space first.
Zero-width spaces (U+200B) are a related but different problem. They are invisible — literally zero width — so they produce no visible gap, but they still sit between characters and break word-matching, substring search, and tokenisation. They typically appear in text copied from certain CMS platforms, rich-text editors, or machine-translated content. Because they are not spaces in any visual sense, removing extra spaces here will not touch them. Use the dedicated zero-width space remover for that.
Three cleanup strategies and when each one backfires
Strategy one: collapse only. Reduce every run of 2+ spaces to a single space, touch nothing else. This is safe for prose and does not affect line structure, indentation at line edges, or punctuation. The risk is that it leaves tabs untouched — if your source mixed tabs and spaces (common in spreadsheet exports), you still have inconsistency after collapsing. Also, it does not touch non-breaking spaces, so U+00A0 runs survive.
Strategy two: trim leading and trailing per line. This targets the edges of each line only, which means internal double spaces stay. It is the safest choice for structured data like CSV — it cleans the padding that tools add for visual alignment without touching the data values themselves. The backfire scenario is indented markup or code: a line that is intentionally indented four spaces will lose its indentation. If your data is a config file or YAML, do not use trim-per-line.
Strategy three: delete all spaces. Every space disappears. This is correct for token-shaped data — product codes, hashtags, identifiers — but will destroy prose completely. One workflow where this appears legitimately: cleaning up OCR output for a field that should be a 16-digit card number. The OCR often inserts spaces between digit groups; delete-all produces the raw number. Anywhere else, verify the use case before applying.
A clean pipeline for most real-world text: (1) run the invisible character remover first to normalise U+00A0, U+3000, and similar variants into U+0020; (2) then collapse here. Two steps, but each tool does exactly one thing and you see the character count change at each stage.
Spaces you should not remove
The single space between a Chinese or Japanese character and a Latin word is meaningful — it is added by convention to improve readability at script boundaries, and removing it glues the scripts together without a break. Collapse mode preserves it because it is already a single space; the only risk is if someone accidentally typed two spaces here, in which case collapsing is still correct.
End-of-line hyphenation in typeset documents sometimes produces a word fragment at the end of one line and the rest of the word at the start of the next. When you collapse those lines into prose, the hyphen acts as the connector. Do not remove or collapse around hyphens — the word is split intentionally.
Initials in names like J. K. Rowling or U.S.A. use single spaces that should stay. Delete-all would destroy these. Collapse does not touch them because they are already single spaces.
Indentation inside code, YAML, and Markdown matters — as covered above. If you are working with a mixed document (prose plus code blocks), copy only the prose section into the tool, clean it, then paste back. Treating the whole file as plain text and running collapse over it is a common source of broken configs.
Batch cleaning: meeting transcripts, Excel exports, LLM output
Meeting transcripts from auto-transcription services often arrive with systematic double spaces between speaker turns, extra spaces inside timestamps, and occasional non-breaking spaces from the web interface used to copy the transcript. Paste the full transcript — 200 paragraphs is fine, the tool processes in-browser with no size limit — apply collapse, check the before/after character count, and copy. A 15,000-character transcript typically drops 300–600 characters from double spaces alone.
Excel column exports are the classic trim-per-line case. When you copy a column of names or codes from Excel, many cells have a trailing space that Excel added during data entry or import. Paste the column, trim each line, then copy back. The character count drops by exactly one per padded cell, so you can spot-check by counting the delta.
LLM output post-processing is a newer use case. Models like GPT-4 occasionally emit double spaces inside a sentence — usually at clause boundaries — especially in longer outputs. If you are feeding LLM output into a pipeline that does exact-match comparisons or stores into a VARCHAR field with a unique constraint, run it through collapse first. The clean AI text tool handles this alongside other AI-specific artifacts like em-dash overuse and hallucinated markdown.
After cleaning spaces, if you want to strip markdown symbols the LLM added, pass the result through the remove markdown tool to get clean plain text. The two tools complement each other: spaces first, then symbols.
Frequently Asked Questions
▸Does collapsing spaces change how the text reads?
No. Collapse mode only affects runs of two or more consecutive spaces. Single spaces, all punctuation, newlines, and every word are passed through unchanged. The sentence "I went home" becomes "I went home" — identical meaning, standard spacing. The only perceptible difference is the removal of the extra gaps.
▸Will this tool affect tabs or newlines?
No. Tabs (U+0009) and newline characters (U+000A, U+000D) are distinct code points from the space character (U+0020) and are not processed by collapse or delete mode. Trim-per-line mode is the exception: it strips both spaces and tabs from the edges of each line, because leading tabs on a prose line are almost always formatting residue rather than intentional indentation.
▸My text came from a PDF and still has odd spacing after collapsing. Why?
PDFs often encode non-breaking spaces (U+00A0), thin spaces (U+2009), and other Unicode space variants instead of ordinary spaces. Collapse mode here targets U+0020 only. To normalise all space variants to a regular space first, run the text through the <a href="/invisible-character-remover" class="text-accent underline underline-offset-2">invisible character remover</a>, which maps every Unicode space variant to a standard space, then collapse here.
▸Can I remove spaces only at the start of each line?
Select Trim each line. That mode strips both leading and trailing spaces from every line independently. If only leading spaces matter and trailing spaces should stay, the output will still have trailing spaces removed — most workflows treat either kind as noise, so this is rarely an issue in practice.
▸What happens to a line that is nothing but spaces?
In collapse mode, a line containing only spaces becomes a single space. In trim-per-line mode, it becomes an empty line. In delete-all mode, it disappears entirely. Choose delete-all if blank-only lines should not survive. If empty lines should be removed but normal lines should stay, run the result through the remove duplicate lines tool with blank-line deduplication enabled.
▸I copied text from a PDF and the spacing still looks wrong after collapsing. What else should I try?
PDFs frequently encode non-breaking spaces (U+00A0), thin spaces (U+2009), figure spaces (U+2007), and other Unicode whitespace variants alongside or instead of regular U+0020 spaces. Collapse mode here only targets U+0020. Run the text through the <a href="/invisible-character-remover" class="text-accent underline underline-offset-2">invisible character remover</a> first — it maps every Unicode space variant to a standard space — then run collapse here. Two passes, but the result will be clean.
▸What is the difference between this tool and the zero-width space remover?
This tool removes visible extra spaces — runs of two or more U+0020 characters, or leading and trailing U+0020 on lines. Zero-width spaces (U+200B) are a completely different character: they are invisible on screen, have zero display width, and sit inside words rather than between them. They do not cause visible double-spacing; they cause broken text search, broken copy-paste, and tokenisation failures. If your text looks correctly spaced but searches fail or character counts seem off, use the <a href="/zero-width-space-remover" class="text-accent underline underline-offset-2">zero-width space remover</a>. If you see actual visible gaps between words, use this tool.
▸Can I use this tool to clean up LLM-generated text that has random double spaces?
Yes — collapse mode is the right setting. Large language models occasionally emit double spaces at clause boundaries, especially in longer completions. Paste the output, select collapse, and the character count will drop by the number of extra spaces removed. If the LLM also added markdown symbols like asterisks, headers, or bullet dashes, clean those out with the <a href="/remove-markdown-from-text" class="text-accent underline underline-offset-2">remove markdown tool</a> afterwards. For a broader cleanup of AI writing habits — em-dashes, filler phrases, formatting artifacts — the <a href="/clean-ai-text" class="text-accent underline underline-offset-2">clean AI text tool</a> covers all of that in one pass.