Toolphy

Remove Line Breaks and Extra Spaces from Text

Last updated:

Pasted text is rarely clean. It arrives with double spaces after sentences, trailing tabs, blank lines between paragraphs, and line breaks that do not match the destination. Cleaning it by hand is slow and inconsistent; recognising the invisible characters makes the job mechanical.

The characters you cannot see

CharacterOften written asWhat it does
Line feed\nStarts a new line (Unix, macOS).
Carriage return + line feed\r\nStarts a new line (Windows).
Carriage return\rOld Mac line ending.
Tab\tInserts a wide gap or column spacing.
Spaceordinary spaceA single blank.
Non-breaking space  / U+00A0A space that refuses to wrap.

The last one is the trap. A non-breaking space looks identical to a normal space but is a different character, so a search for " " (space) will not find it and some cleanup tools leave it behind.

Before and after

Suppose the pasted text is:

  Hello   world.
This   is a line


with blank   rows.  

After collapsing runs of spaces to one, trimming each line, and removing blank lines, it becomes:

Hello world.
This is a line
with blank rows.

Three separate operations did that: trim (strip leading and trailing whitespace per line), collapse (turn two or more spaces or tabs into one), and remove empty lines. Running them in that order gives a predictable result.

A safe cleaning recipe

  1. Normalise line endings first. Convert every \r\n and \r to \n so later steps see one kind of break.
  2. Replace non-breaking spaces. Swap U+00A0 for a normal space before collapsing, or it will survive.
  3. Collapse spaces and tabs. A single pass turning any run of whitespace within a line into one space.
  4. Trim each line. Remove the space left at the start or end.
  5. Remove blank lines if you want a compact block, or keep single blank lines if paragraphs must stay separated.

When to remove breaks and when to keep them

Removing every line break turns a list into one longline, which is useful when a field expects a single value (a spreadsheet cell, an address line, an API parameter). It is harmful when the breaks carry meaning: poetry, code, CSV rows and step-by-step lists all lose structure. Decide from the destination, not the habit. If paragraphs matter, keep one blank line between them and remove the rest.

Common mistakes

Paste your text into our remove duplicate lines tool to trim, collapse and de-duplicate in one pass. To check exactly what changed between the messy original and the cleaned version, run both through the text comparison guide. If the text is bound for a structured format, the CSV to JSON guide explains how line breaks behave there.

Try the Remove Duplicate Lines →