Collapse Whitespace Tool

Paste text to collapse runs of whitespace into single spaces.

How it is calculated

The tool replaces every run of one or more whitespace characters with a single space, using the pattern \s+ in Unicode mode, and then (by default) trims the space left at the start and end. Whitespace here means ordinary spaces, tabs, line breaks and also non-breaking or ideographic spaces, which become a normal space. Lengths are counted in characters, not bytes, so accented letters count as one. The removed figure is original length - new length. With trimming switched off, a run at the start or end collapses to one space instead of disappearing. An empty input gives no result. Text made only of whitespace returns an empty string when trimming is on.

Worked example

Input: two spaces, Hello, three spaces, world,, two line breaks, a tab, this, two spaces, is, three spaces, a, two spaces, test., two trailing spaces. The tool reports 40 chars. Each run becomes one space, and the leading and trailing runs are trimmed. Result: Hello world, this is a test. The new length is 28 chars, so 12 chars were removed. With trim off, the result keeps one space at each end: 30 chars, 10 chars removed.

Try this example →

Common mistakes

  • Expecting line breaks to survive. Paragraph breaks are also whitespace, so multi-line text becomes one single line.
  • Using it to clean code or indented lists. Indentation is collapsed too, so it is not suitable for Python, YAML or tables aligned with spaces.
  • Turning trim off and then wondering about a leftover space at the start or end. Each edge run becomes one space.
  • Expecting zero-width characters to disappear. Only characters treated as whitespace are collapsed; hidden zero-width marks are not guaranteed to be removed.
  • Comparing the removed count to the number of spaces you see. It counts all characters dropped, including tabs and line breaks.

About this calculator

This tool applies a simple but common text-normalization pattern: it matches every run of one or more whitespace characters — spaces, tabs, and line breaks alike, typically via a regular expression like /\s+/ — and replaces each run with a single space, then trims any leading or trailing space from the result.

Text pasted from PDFs, emails, or scraped web pages often carries stray tabs, double spaces, and inconsistent line breaks that break string comparisons, CSV parsing, or search matching. Developers cleaning input data, data-entry and QA teams, and copywriters preparing text for publishing use whitespace collapsing to normalize messy text into a single clean line before it's stored, compared, or displayed.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    Text copied from a PDF often breaks words with a hyphen at the end of a line. This tool joins the lines with a single space but leaves the hyphen alone, so you may still see something like exam- ple in the output. Collapse first, then search for a hyphen followed by a space and fix those by hand. Which source do you most often clean up: PDFs, emails or scraped pages?

Popular calculators

All Calculators