Remove Extra Spaces

Clean up messy spacing in text.

How it is calculated

The tool splits your text into lines at each line break, then on every line it replaces any run of spaces and tab characters with a single space and removes spaces at the start and end of the line. The lines are then joined back with ordinary line breaks. So the words and the number of lines stay the same, and blank lines stay as blank lines rather than being deleted. Only the plain space and the tab are handled: non-breaking spaces and wide spaces such as the ideographic space are left exactly as they are, as are line breaks inside the text. Empty input gives no result. Use it before pasting text into a form, a spreadsheet or code.

Worked example

Suppose you paste the text with 3 spaces at the start, 5 spaces between the first two words, 3 spaces between the last two and 3 trailing spaces: Hello world again. The tool collapses every run of spaces to one and trims both ends, which returns Hello world again. A tab between two words would be replaced by a single space in the same way.

Try this example →

Common mistakes

  • Expecting blank lines to disappear. Empty lines are kept, so a gap of three blank lines stays three blank lines.
  • Expecting non-breaking spaces from web pages or Word documents to be collapsed. They look like spaces but are different characters, and the tool leaves them alone.
  • Using it to align columns. Collapsing runs of spaces removes the padding that made text columns line up.
  • Applying it to code where indentation matters, such as Python or YAML, since leading spaces on every line are removed.
  • Expecting line breaks to be joined into one line. Each line is cleaned separately and the breaks are kept.

About this calculator

This tool cleans up messy whitespace in a block of text by collapsing any run of repeated spaces or tabs down to a single space and trimming leading and trailing whitespace from each line, without touching the actual words or line breaks.

It is used to fix text that picked up extra spacing from copy-pasting out of a PDF, a formatted email, or an old document with inconsistent tabs, and by writers, developers and data-entry workers who need clean, normalized text before pasting it into a form, a spreadsheet cell, or code where stray whitespace can cause formatting or parsing problems.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    Be careful with data copied from a spreadsheet. Rows pasted from Excel are separated by tab characters, and this tool turns every tab into one plain space, so the columns merge into a single line of text. Use it for prose, addresses and single cells, not for tables. Which kind of text do you usually need to clean up?

Popular calculators

All Calculators