Trim Whitespace

Strip leading and trailing whitespace from every line. Internal runs of spaces collapse to a single space. Cleans up text exported from PDFs, spreadsheets, and forms.

Example: hello world foo bar hello world foo bar

Strips leading and trailing whitespace (spaces, tabs) from every line, and collapses runs of internal whitespace into single spaces. The most common normalisation step before further processing.

Especially useful for text copied from PDFs (which often arrives with stray double-spaces), spreadsheet exports, or form-pasted user input.

When you reach for this

Cleaning pasted form data

User-entered emails and names often arrive with leading/trailing spaces from copy-paste. Trim before storing or comparing.

Normalising PDF text

PDF copy-paste produces erratic spacing. Trim and collapse to get a clean string.

Pre-deduplication step

Before removing duplicates, trim — otherwise "hello" and "hello " stay distinct.

Spreadsheet column cleanup

Copy a column out of Excel, trim, paste back — fixes alignment issues from inconsistent spacing.

Search-string preparation

Trimming queries before sending them to a search API avoids "no results" caused by trailing whitespace.

What gets processed (and how)

The tool reads the input text and splits it into lines using the newline character (LF or CRLF). For each line, it applies two operations: trimming and internal collapse. Trimming removes all leading and trailing whitespace characters—spaces (U+0020), tabs (U+0009), and other Unicode whitespace like non-breaking spaces (U+00A0) if present — using a Unicode-aware regex /^[\s]+|[\s]+$/gu. Then, internal whitespace runs (contiguous spaces/tabs) are collapsed to a single space using /[\s]{2,}/g, replacing with a space. Empty lines (containing only whitespace) become truly empty lines. The tool processes lines independently, preserving line count.

How to use it

  1. Paste your text into the input box.
  2. Click the 'Trim Whitespace' button.
  3. The tool automatically splits lines, trims each, and collapses internal spaces.
  4. Copy the cleaned output from the result box.

Behavior on tricky inputs

Empty lines
Lines containing only whitespace become empty strings, preserving the line break.
Trailing newline
A trailing newline at end of input is ignored; output ends with a newline if input did.
CRLF vs LF
The tool handles both CRLF and LF line endings, converting internally to LF for consistency.
Non-breaking spaces
Non-breaking spaces (U+00A0) are treated as whitespace and removed/collapsed like regular spaces.

Workflow tips

  • Use this tool after extracting text from PDFs to remove erratic spacing and line-level indentation.
  • Paste data from a spreadsheet column; the tool trims each row and collapses internal tabs into single spaces.
  • If you need to preserve internal tabs, run the trim step only (available in advanced options).
  • Combine with line sorting or deduplication tools on this site for a complete text cleanup pipeline.

vs other ways to do this

Three ways to trim whitespace from text: this web tool, a Unix sed one-liner, and Excel's TRIM function.

This toolsed (Unix)Excel TRIM
PlatformWeb browser, no installCommand line (Linux/macOS/WSL)Microsoft Excel
Whitespace handlingUnicode-aware, collapses internal runsASCII-only unless extended, can collapseCollapses internal spaces, ASCII only
Line-by-lineYes, automaticYes, with explicit line addressingNo, per-cell only; requires split/combine
Multiple lines in one goWorks on any pasted blockWorks on entire file or pipeNeeds helper columns or VBA

Where this came from

Whitespace trimming is one of the oldest text processing operations, dating back to early Unix tools like sed (1974) and awk (1977). The concept originates from the need to clean up terminal output and text files where indentation or trailing spaces were artifacts of line editors. The specific pattern of trimming leading/trailing whitespace and collapsing internal runs was formalized in utilities like fmt (1979) and later adopted in spreadsheet functions like Excel's TRIM (1990s). This web tool brings that classic data-cleaning step to the browser without requiring a terminal or formula knowledge.

Common questions

Does it remove tabs as well as spaces?

Yes. Tabs, spaces, and any other Unicode whitespace are all stripped.

Will it collapse internal whitespace too?

Yes — runs of two or more whitespace characters between words collapse to a single space. If you need to preserve double-spaces, do the leading/trailing trim manually.

Does it remove blank lines?

No — empty lines stay as empty lines. Use Remove Blank Lines for that.

Will it affect non-breaking spaces?

Yes — JavaScript's \s regex matches non-breaking spaces, so they're collapsed too. If you need to preserve  , use a custom regex.

Embed our tools on your website

Free for any site. No signup. Iframe loads from our servers and stays up-to-date automatically.

📋 Embed the Word Counter

Copy this snippet (auto-resizes, Copy button works):

Live preview:

📋 Embed this Trim Whitespace

Copy this snippet (auto-resizes, Copy button works):

Live preview:

Want more options? All embeddable tools →