My Tool Studio
Text

Trim Whitespace

Paste text or open a file and this tool will trim whitespace from every line at once: spaces, tabs and non-breaking spaces at the start of each line, at the end, or both, or every space inside the lines too. Line breaks stay, and blank lines can be kept, reduced to one or removed. Options collapse double spaces between words without touching indentation, turn odd spaces into normal ones and strip zero-width characters. A counter shows how many characters were removed. The typical case is a list pasted from a spreadsheet or a form where invisible padding makes lookups, comparisons or imports fail, because "abc " is not the same as "abc". Developers use it on config values and test data, and anyone preparing a CSV import uses it to stop duplicates caused by stray spaces. It runs in your browser.

Always freeNo sign upRuns in your browser

How to use

01

Paste the padded text

Paste into the Text with extra whitespace box, or press Try sample to load lines with leading spaces, trailing spaces, a tab and double spaces between words.

02

Choose what to trim

In the Trim menu pick Both ends, Leading only, Trailing only or All spaces and tabs. Choose what happens to Blank lines, and tick the collapse, odd spaces or zero-width options if needed.

03

Check and copy

The Trimmed text pane updates as you type, and the counter shows how many characters were removed. Press Copy, or Download .txt to save the clean text.

Why Trim Whitespace

Before and after: leading, trailing and inner spaces

In this table a middle dot stands for a space and an arrow for a tab, so you can see what each setting removes. The collapse option is on in the last column only. Notice that collapsing never touches the tab at the start of a line, and that Trailing only keeps both the tab and the leading spaces, which is why it suits code.

Input lineBoth endsLeading onlyTrailing onlyBoth ends plus collapse
···Leading spacesLeading spacesLeading spaces···Leading spacesLeading spaces
Trailing spaces·····Trailing spacesTrailing spaces·····Trailing spacesTrailing spaces
→A tab and···double···spaces··A tab and···double···spacesA tab and···double···spaces··→A tab and···double···spacesA tab and double spaces
··Both ends padded··Both ends paddedBoth ends padded····Both ends paddedBoth ends padded

Common questions

What is the difference between trimming and removing all spaces?
Trimming only removes whitespace at the start and end of each line, so the spaces between words stay. Removing all spaces joins every word together, which you can do by choosing All spaces and tabs, inside lines too, for example to clean phone numbers or codes. To shrink double spaces between words without joining them, tick the collapse option instead.
How do I remove double spaces between words?
Tick Collapse multiple spaces between words to one. Any run of two or more spaces, tabs or no-break spaces between two visible characters becomes a single space. Indentation at the start of a line is not affected by this option.
Does trim remove non-breaking spaces?
Yes, at the start and end of lines. The trim uses JavaScript's definition of whitespace, which includes the no-break space U+00A0 and other Unicode space characters. Tick Turn non-breaking and other odd spaces into normal spaces to fix the ones inside lines too, and Remove zero-width characters for U+200B, U+200C, U+200D, U+2060 and U+FEFF.
How do I remove leading and trailing spaces in Excel?
Use =TRIM(A1) in a helper column, then copy and paste the results as values. Excel's TRIM also reduces spaces between words to one, but it does not remove the no-break space, CHAR(160), which is common in text copied from web pages. For that, wrap it as =TRIM(SUBSTITUTE(A1,CHAR(160)," ")), or paste the column here.
Can I trim only the start or only the end of each line?
Yes. Choose Leading only to remove indentation while keeping trailing spaces, or Trailing only to remove the invisible spaces at line ends while keeping indentation. Trailing only is the safe choice for code and Markdown lists.
Will trimming delete my blank lines?
Not unless you ask it to. With Blank lines set to Keep them, a line with only spaces becomes a truly empty line but stays, so paragraph gaps remain. Choose Keep one between blocks to shrink runs of blank lines to one, or Remove them to delete every blank line.
Why do trailing spaces cause errors in imports and logins?
Because computers compare text exactly. abc with a trailing space is a different value from abc, so lookups miss, passwords fail and imports create duplicate records. Trimming before you compare or import prevents that.
Does trimming whitespace affect tab indentation in code?
Both ends and Leading only remove tabs at the start of a line, which flattens indentation in code, Python and Makefiles. Use Trailing only for source code so the indentation stays.

More Text tools

View all