Guide
How to Trim Whitespace in Excel and Code
Trim leading and trailing spaces with Excel TRIM, JavaScript trim(), Python strip() and SQL TRIM, and find the trailing spaces that break matches.
What Is Trim Text?
Trim Text will trim whitespace from every line. It will remove leading spaces, remove trailing spaces, and strip whitespace from the start and end of each line: " hello world " becomes "hello world". This per-line trim lets you trim lines in bulk, and invisible trailing spaces still get removed even when you cannot see them. The words and the spacing between them stay exactly as they were.
Try Trim Text →Why Trailing Whitespace Causes Bugs
Whitespace at the ends of a line is invisible but consequential. It breaks exact comparisons, causes coupon codes and passwords to fail validation, misaligns data in spreadsheet columns, and trips linters in source code. Leading whitespace is indentation, so trimming is also how to strip indentation from pasted code when you want it flush left. Trimming is one of the most common normalisation steps before storing or comparing text.
How It Works
The tool processes each line independently, stripping leading and trailing whitespace and leaving the interior of the line alone. This is a per-line trim vs whole-text trim: every line gets its own edges cleaned, rather than trimming only the very start and end of the whole block. For example, " hello world " becomes "hello world": the interior spacing is preserved, only the ends are cleaned.
This tool is free and runs online entirely in your browser. Your text stays on your device, protecting your privacy: what you enter is not sent to a server, and results appear instantly.
Examples
A padded coupon code. For example, " SAVE20 " becomes "SAVE20", so the code matches when a form checks it exactly.
Indented paste. For example, a block of code copied with four leading spaces per line comes back flush left once the leading whitespace is trimmed.
How Do You Trim Whitespace in Code?
Most languages ship a trim built in. JavaScript has trim() for both ends, plus
trimStart() and trimEnd(). Python has strip(),
lstrip() and rstrip(). SQL has TRIM(), and most
databases also offer LTRIM() and RTRIM().
Those functions trim one string. To trim every line, as this tool does, split first:
text.split('\n').map(line => line.trim()).join('\n') in JavaScript, or
'\n'.join(line.strip() for line in text.splitlines()) in Python. Note that SQL's
plain TRIM() usually removes spaces only, so a trailing tab can survive it.
How Is Excel TRIM Different From This Tool?
Excel's TRIM removes leading and trailing spaces, and it also squeezes runs of spaces inside
the cell down to one. =TRIM(" A 1 ") returns "A 1", not "A 1". This tool
leaves the inside of each line alone and only cleans the edges.
That difference matters when inner spacing carries meaning, such as aligned codes or fixed-width columns. TRIM also ignores the non-breaking space (character 160) that web pages leave behind, so a cell can still look padded after TRIM runs.
Why Does a Trailing Space Break a Match?
A trailing space makes two values look equal when they are not. In JavaScript,
"SAVE20 " === "SAVE20" is false. In Excel, a lookup for "SAVE20" returns
#N/A when the table holds "SAVE20 " with a space at the end. Nothing on screen shows the
difference.
In source code, trailing spaces add noise to version control. A line that differs only by a
trailing space still shows up as changed, and git diff --check reports it as a
whitespace error. Trimming line ends before you commit keeps diffs about real changes.
Common Mistakes
Trim Text vs Other Whitespace Tools
Trimming edges vs collapsing internal spaces is the distinction to keep in mind. Trim Text is the most targeted of the whitespace cleanup alternatives: it only touches the edges of each line and preserves everything else. Remove Extra Spaces is more thorough: it also collapses runs of spaces inside each line. Normalize Whitespace is the most aggressive alternative: it removes all whitespace runs and line breaks, collapsing the entire text to a single space-separated line. Remove Blank Lines deletes empty rows but leaves line content alone. A common pattern is to run Trim Text first to clean the edges, then Remove Extra Spaces to clean the interior.
Related Tools
You May Also Need
You may also need
- Uppercase ConverterChange the case once the stray whitespace is gone
- Remove Extra SpacesCollapse internal runs after trimming the edges
- Remove Duplicate LinesTrim first so duplicates match exactly
Next steps
- Remove Blank LinesDrop the now empty lines trimming exposed
Alternatives
- Normalize WhitespaceStandardize all whitespace at once