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.
hello world
foo bar → hello world
foo bar
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.
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.
User-entered emails and names often arrive with leading/trailing spaces from copy-paste. Trim before storing or comparing.
PDF copy-paste produces erratic spacing. Trim and collapse to get a clean string.
Before removing duplicates, trim — otherwise "hello" and "hello " stay distinct.
Copy a column out of Excel, trim, paste back — fixes alignment issues from inconsistent spacing.
Trimming queries before sending them to a search API avoids "no results" caused by trailing whitespace.
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.
Three ways to trim whitespace from text: this web tool, a Unix sed one-liner, and Excel's TRIM function.
| This tool | sed (Unix) | Excel TRIM | |
|---|---|---|---|
| Platform | Web browser, no install | Command line (Linux/macOS/WSL) | Microsoft Excel |
| Whitespace handling | Unicode-aware, collapses internal runs | ASCII-only unless extended, can collapse | Collapses internal spaces, ASCII only |
| Line-by-line | Yes, automatic | Yes, with explicit line addressing | No, per-cell only; requires split/combine |
| Multiple lines in one go | Works on any pasted block | Works on entire file or pipe | Needs helper columns or VBA |
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.
Yes. Tabs, spaces, and any other Unicode whitespace are all stripped.
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.
No — empty lines stay as empty lines. Use Remove Blank Lines for that.
Yes — JavaScript's \s regex matches non-breaking spaces, so they're collapsed too. If you need to preserve , use a custom regex.