How To Trim Whitespace From Text

Learn what trimming does, why trailing spaces cause bugs, and how to clean the start and end of every line at once.

3 min read Updated Jun 2026

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: nothing is uploaded 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.

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.

You May Also Need

You may also need

Next steps

Alternatives

Continue Learning