Trim whitespace

Spaces off the ends of every line — and a count of the lines that end in something invisible, which is the one kind of space nothing will trim for you. Nothing you type here leaves your browser. It is worked out on your own device, so there is nothing to send and nothing to log — which matters more for text than for most things, because people paste in work they are not supposed to paste anywhere.

The only space it will not take is the one you cannot see

Fifteen characters in Unicode behave like a space. JavaScript's trim() removes fourteen of them — including the no-break space, the em space, the ideographic space and the byte order mark.

The one it leaves is U+200B ZERO WIDTH SPACE. It is category Cf, a format character rather than a separator, so by the standard's own reckoning it is not whitespace at all. It also has no width, which means a line ending in one looks exactly like a line that does not.

That combination is the problem. The line looks clean, the tool says it trimmed it, and every comparison afterwards says the two strings differ. Zero width spaces arrive from copied web pages, from some PDF exports and from software that uses them as line break hints.

And "trimmed" depends on what trimmed it

Python's strip() and JavaScript's trim() do not agree. Both leave the zero width space. Python also leaves U+FEFF, the byte order mark, which JavaScript removes.

So text trimmed in a browser can still carry a character that a Python script will find, and vice versa. Trimmed is not a property of the text. It is a property of whatever did the trimming, and this page says which it used.

Which end

Leading and trailing are separate choices because they are separate jobs. Stripping the start of every line destroys indentation, which is exactly what you want for pasted prose and exactly what you do not want for code.

Nothing here touches spaces inside a line. Collapsing runs of spaces in the middle changes the text rather than tidying it, and that is a different operation.

Where to go next