UTILYARD
guides

How to Remove Whitespace from Text

What whitespace characters are, where messy whitespace comes from, and the right cleanup operation for each situation.

What is whitespace?

Whitespace is any character that takes up space but displays nothing visible. The most common types are:

CharacterEscape sequenceWhat it looks like
SpacenoneThe most common — between words
Tab\tWide indent, common in code and TSV files
Newline (LF)\nLine break on Unix/Mac/modern systems
Carriage return (CR)\rOld Mac / Windows line endings (paired with \n)
Non-breaking space\u00A0Copied from web pages, looks identical to a space but behaves differently

Non-breaking spaces are a common gotcha — they're invisible to the eye but cause string comparisons, word counts, and database lookups to fail in unexpected ways.

Where messy whitespace comes from

Whitespace problems almost always come from one of these sources:

  • PDFs: Copy-pasting from a PDF frequently adds extra spaces between words and random line breaks mid-sentence.
  • Websites: HTML is rendered with collapsed whitespace, but the underlying source often has leading spaces and extra blank lines that copy along with the text.
  • Email clients: Plain-text emails often pad lines to a fixed column width, inserting trailing spaces on every line.
  • Spreadsheets: Cells exported to CSV or pasted into text often have leading/trailing spaces that cause sort and match failures.
  • Legacy systems: Fixed-width databases and mainframe exports frequently pad fields with trailing spaces to a fixed column count.

The five cleanup operations

Each operation targets a specific type of whitespace problem. Use the one that matches what you're cleaning.

1. Trim lines — remove leading and trailing spaces from each line

The most common operation. Removes spaces (and tabs) at the very start and end of every line. Does not affect spacing within lines or between lines.

"   Hello world   "  →  "Hello world"

Best for: spreadsheet exports, copy-pasted table data, fixing padded fields.

2. Collapse spaces — replace multiple consecutive spaces with one

Scans each line and replaces any run of two or more consecutive spaces with a single space. Preserves single spaces and newlines.

"Hello   world    today"  →  "Hello world today"

Best for: PDF copy-paste, email body text with extra word spacing.

3. Replace tabs — convert tab characters to a single space

Replaces every tab character with a single space. Use this before collapsing spaces for a two-pass cleanup of tab-indented data.

"Name	Age	City"  →  "Name Age City"

Best for: TSV files, code-formatted data, log files with tab separators.

4. Remove blank lines — delete empty or whitespace-only lines

Filters out any line that contains nothing, or only whitespace. Useful for tightening up text that has large gaps between paragraphs.

Line one


Line two  →  Line one
Line two

Best for: multi-line copy-paste from websites, email threads, raw exported text.

5. Remove ALL whitespace — strip every whitespace character

Removes every space, tab, and newline from the text. The result is one unbroken string. This is destructive — use it only when you specifically want a compact string (e.g. extracting digits from a formatted number).

"1 234 567.89"  →  "1234567.89"

Best for: parsing formatted numbers, extracting a pure character sequence, minification.

Which operations to combine

Multiple operations can be applied together. Here are the most useful combinations:

ScenarioOperations to use
Clean up PDF copy-pasteTrim lines + Collapse spaces + Remove blank lines
Normalize a data fieldTrim lines + Collapse spaces
Process a TSV/tab-separated fileReplace tabs + Trim lines
Prepare text for a database insertTrim lines + Collapse spaces
Extract pure charactersRemove ALL whitespace
Try it: Whitespace Remover
Paste your text, toggle the options you need, and copy the cleaned result.
Open tool →

Frequently asked questions

Why does trimming fail on text copied from a website?
Web pages often contain non-breaking spaces (\u00A0) instead of regular spaces. Non-breaking spaces look identical to regular spaces but are not matched by most standard trim operations. If trimming doesn't work, the culprit is usually a non-breaking space — the "Remove ALL whitespace" option catches these since it targets all Unicode whitespace characters.
What is the difference between trimming and collapsing spaces?
Trimming removes spaces at the edges of a line — the leading and trailing whitespace. Collapsing removes extra spaces in the middle of a line by reducing consecutive runs of spaces to a single space. They solve different problems and are often used together.
Will removing blank lines join my paragraphs together?
No. Removing blank lines removes empty lines, but lines that have content are left intact. Two consecutive content lines will remain as two separate lines — they just no longer have empty lines between them.
Can I undo the changes?
The original text is preserved in the input box — the output is always a separate field. Simply edit or clear the input to start over. Nothing is sent to a server, so there's no server-side state to worry about.