All articles

Whitespace and invisible characters: the hidden cause of weird bugs

Copy a heading or a short phrase out of Google Docs or Microsoft Word and paste it into an input field, and there's a decent chance you've just imported a non-breaking space (U+00A0, the same character as the HTML entity  ) disguised as a regular one — word processors insert it automatically in places where a line break would look awkward.

Why this causes strange bugs

The classic symptom is a string comparison that unexpectedly returns "not equal," even though the text on screen looks identical. The cause is often that one string contains a regular space while the other contains that imported non-breaking space. Visually there's no difference; to the computer, there is — and this is exactly the kind of bug that survives a code review because nobody can see it in a diff.

Trimming

Trim removes whitespace characters from the start and end of a string, leaving the content in between untouched. This is a standard input-cleaning operation — an accidental extra space at the start of a form field shouldn't affect how a value is compared or validated.

Collapsing whitespace

Another common operation is replacing several consecutive whitespace characters with one. This is useful for text copied from a formatted source (a PDF or a table, say), where accidental double spaces or tabs can end up where a single space was intended.

Why you need this

  • Diagnosing why a comparison of two seemingly identical strings fails.
  • Cleaning text pasted from a word processor or web page of stray, invisible characters like non-breaking spaces.
  • Normalizing user input before saving it to a database.

Zero-width characters

Besides visible whitespace characters, there are zero-width characters (zero-width space, zero-width joiner) — they take up no space on screen at all, yet are present in the text as distinct characters. These sometimes get copied in from certain websites or messengers and can silently break string comparison or search, while remaining completely invisible even in a text editor.

Try the tool