belun.app Blog
RU

Extra Spaces in Text: Where They Come From and How to Strip Them

Why pasted text is full of double spaces and invisible characters, what breaks because of them, and how to clean a document in a few seconds.

Clean sheet of white paper on a minimal desk, illustrating tidy text after removing extra spaces

A VLOOKUP that refuses to match. A search that finds nothing even though the word is right there on screen. A name field that sorts wrong. Nine times out of ten the culprit is a space you can’t see.

The Whitespace Remover strips them out, and shows you how many it found.

Where the mess comes from

Typing habits are the obvious source. Anyone taught to type on a typewriter puts two spaces after a period, and that convention outlived the machines by fifty years. Word processors also insert them: hitting Tab in a table cell, letting autocorrect fix a line break, pasting a bullet list.

The nastier source is copy-paste. When you copy from a PDF, a Word document, or a rendered web page, you don’t get the characters you see. You get whatever the layout engine used to push things apart:

  • Non-breaking space (U+00A0), which browsers and Word insert to stop lines wrapping in awkward places
  • En and em spaces (U+2002, U+2003), used for typographic spacing
  • Ideographic space (U+3000), standard in Japanese and Chinese text
  • Zero-width joiner and zero-width non-joiner, invisible characters with actual width of nothing
  • Byte-order mark (U+FEFF), which loves to attach itself to the first character of a file

Every one of these renders like a space, or like nothing at all, and none of them is a space as far as your code is concerned. "John Smith" === "John Smith" returns false when one of those gaps is U+00A0. That’s the bug, and you can stare at both strings all afternoon without seeing it.

What actually breaks

CSV imports split on the wrong column. Database lookups miss rows. TRIM() in Excel only strips regular ASCII spaces, so it happily leaves U+00A0 in place, which is why the classic advice to “just use TRIM” fails on data copied from a website.

URL slugs come out with %C2%A0 in the middle. Regex patterns that use \s do catch most of these in JavaScript, but a hand-written [ \t] pattern doesn’t. And in a diff, two lines that look identical show as changed, which is maddening during code review.

Cleaning it

Paste the text into the left box and the cleaned version appears on the right as you type. The default rules cover most cases:

Repeated spaces collapse to one. Every line gets trimmed at both ends. Runs of blank lines merge into a single blank line, so paragraph breaks survive but the six-line gap left by a bad paste doesn’t. Unicode lookalikes become plain spaces, and zero-width characters get deleted outright.

The rest is opt-in. “Remove empty lines” packs everything together with no gaps, which is what you want before pasting into a single spreadsheet cell. “Convert tabs to spaces” helps with code copied out of a terminal. “Remove all whitespace” flattens everything into one unbroken string, useful for comparing hashes or checking a base64 payload.

The counters below show characters, lines, and words before and after, plus how much was stripped. On a 4,000-word document pasted out of Google Docs, seeing “removed 312 characters, 1.9%” is a decent sanity check that the tool did something and didn’t eat your text.

One caveat

Trimming lines will flatten indentation. If you’re cleaning source code or a YAML file, leave “Trim each line” off, or you’ll break it. For prose, trim away.

The text stays in your browser the whole time. Nothing gets uploaded, so contracts and client documents are fine to paste in.

Clean up a document now and see what was hiding in it.

Try the tool

Whitespace Remover →