Identify Non-Whitespace Characters

How to tell whitespace from non-whitespace characters in Unicode, why invisible characters are not always whitespace, and when cleaning text helps or harms.

Finding the non-whitespace characters in a string sounds simple: anything that is not a space, tab or line break. In Unicode it is less tidy. Some characters that look blank are not classified as whitespace. Some characters that act like spaces are not the ordinary space. Programming languages also disagree about which characters a whitespace test should match. This review explains what counts, how to inspect text at the code point level, and when changing the text is a good idea.

Is inspecting or cleaning non-whitespace characters right for your use?

Inspection is almost always worth doing when text behaves oddly. Common signs are a search that fails on a word that looks correct, a password or code that will not paste, a duplicate entry that looks identical to another, or source code that compiles differently from how it reads. Listing each code point with its name and category shows what is actually stored. It does not change the text.

Cleaning is where the tradeoffs appear. Removing or replacing invisible characters fixes many matching and parsing problems. It can also break text that depends on them. The zero width joiner holds many emoji sequences together. The zero width non-joiner affects correct spelling in languages such as Persian. Directional marks keep mixed right-to-left and left-to-right text in the intended order. A blanket rule such as deleting everything invisible is only safe for narrow data: identifiers, codes, URLs or fields you know should contain plain ASCII or a defined character set.

Plain text with only ordinary spaces and line breaks is the better target for usernames, product codes, keys, search terms and anything that will be compared character by character. Preserving the original characters is better for natural-language prose, multilingual content and emoji, where the invisible characters often carry meaning.

What counts as whitespace, and what does not

Unicode assigns a White_Space property to a defined set of characters. The set includes the ordinary space, tab, line feed, carriage return, vertical tab, form feed, next line, no-break space, the range of typographic spaces from en quad through hair space, the line and paragraph separators, the narrow no-break space, the medium mathematical space, the Ogham space mark and the ideographic space. Everything else is a non-whitespace character by that definition, including several characters that render as nothing or as an empty gap.

Character | Code point | Unicode White_Space | Typical appearance
Space | U+0020 | Yes | Ordinary gap
No-break space | U+00A0 | Yes | Gap that resists line wrapping
Ideographic space | U+3000 | Yes | Wide gap, common in CJK text
Zero width space | U+200B | No (format character) | Nothing visible
Zero width joiner | U+200D | No (format character) | Nothing visible; joins emoji and scripts
Zero width no-break space / byte order mark | U+FEFF | No (format character) | Nothing visible
Braille pattern blank | U+2800 | No (symbol) | Empty gap
Hangul filler | U+3164 | No (letter) | Empty gap

The practical lesson is that "invisible" and "whitespace" are separate questions. A whitespace check will not catch a zero width space, and a visual scan will not catch it either. Only code point inspection reliably shows it.

What a non-whitespace check actually tests

A non-whitespace test reads stored characters. It does not look at fonts, styling or how the text is drawn. Bold or italic formatting applied through a word processor or CSS does not change the characters, so it does not affect the result. Text that has been restyled using Unicode substitutes, such as mathematical alphanumeric letters that imitate bold or script fonts, really does contain different characters. Those characters are non-whitespace, but they are not the ordinary letters a search or comparison expects.

The definition of whitespace also depends on the tool. These differences are documented language behavior:

  • JavaScript: the \s regular expression class covers Unicode space separators, line terminators and U+FEFF, so \S treats U+FEFF as whitespace even though Unicode does not. U+200B is not matched by \s.
  • Python 3: str.isspace() and \s in str patterns follow Python's own Unicode-based rule, which also counts the ASCII information separators U+001C to U+001F. U+200B and U+FEFF are not whitespace.
  • Java: by default \s matches only ASCII space, tab, line feed, vertical tab, form feed and carriage return, so a no-break space counts as non-whitespace unless Unicode character class matching is enabled.

If two systems disagree about whether a field is blank or whether two strings match, a different whitespace definition is a likely cause. Choose the definition deliberately rather than relying on a default.

Inspecting text at the code point level

Python's standard library can list each character with its code point, general category and name. This small function marks each one using Python's whitespace rule:

import unicodedata

def inspect(text):
    for ch in text:
        cp = f"U+{ord(ch):04X}"
        cat = unicodedata.category(ch)
        kind = "whitespace" if ch.isspace() else "non-whitespace"
        name = unicodedata.name(ch, "<no name>")
        print(cp, cat, kind, name)

inspect("a\u00a0b\u200bc")
U+0061 Ll non-whitespace LATIN SMALL LETTER A
U+00A0 Zs whitespace NO-BREAK SPACE
U+0062 Ll non-whitespace LATIN SMALL LETTER B
U+200B Cf non-whitespace ZERO WIDTH SPACE
U+0063 Ll non-whitespace LATIN SMALL LETTER C

The output shows both traps at once. The no-break space looks like a normal space but is a different character. The zero width space is invisible but is counted as non-whitespace. Control characters such as tab have no formal Unicode name, which is why the function supplies a fallback label.

The general category is often the most useful clue. Zs marks space separators, Cf marks format characters such as zero width and directional controls, Cc marks control characters, and Mn marks combining marks that attach to a preceding letter.

Combining marks, emoji and why counts can surprise you

Iterating by code point does not match what a reader sees as one character. An accented letter can be stored as a single precomposed character or as a base letter followed by a combining mark, and the second form appears as two non-whitespace code points. Many emoji, including family and profession sequences and some flags, are several code points joined by U+200D or built from paired indicators. Each part shows up separately in an inspection.

This matters when cleaning. Removing U+200D from an emoji sequence splits it into separate emoji. Removing a combining mark changes the letter. Unicode normalization can convert between precomposed and decomposed forms without changing meaning, and it is usually a safer first step than deleting characters. If you need counts that match what users see, count grapheme clusters rather than code points.

Directional controls: non-whitespace characters that reorder display

The left-to-right and right-to-left marks, the embedding and override controls, and the directional isolates are all non-whitespace format characters with no visible glyph. They change the order in which text is displayed, not the order in which it is stored. In multilingual prose they are legitimate. In source code, configuration files or identifiers they can make text appear to say something other than what the computer reads, which is why many code review tools and editors flag them. For code and data files, treating unexpected directional controls as an error is a reasonable default.

Copying, sharing, search and accessibility

Invisible non-whitespace characters generally travel with copied text, because copying moves stored characters. That is why a stray zero width space can follow a code from a web page into a login form and cause a failure that is hard to see. Some applications strip or replace certain characters on paste or save, but behavior varies by destination, and no destination-specific testing was available for this review. Check the specific platform you care about rather than assuming one result applies everywhere.

Search and matching are affected directly. A word containing a zero width space, a no-break space or a styled Unicode substitute letter usually will not match the plain version unless the search system normalizes or ignores those characters.

For assistive technology, characters that look blank but are classified as symbols or letters, such as the braille pattern blank or the Hangul filler, may be handled differently from real spaces. Screen readers and settings differ, and no scoped screen reader testing was available here, so specific announcements cannot be stated. Where readability for all users matters, a plain-text equivalent using ordinary spaces and letters is the dependable choice.

How to find a tool for this

Useful search terms include "Unicode character inspector", "code point viewer", "show invisible characters" and "zero width character detector". Many code editors offer an option to render whitespace and to highlight invisible or ambiguous Unicode characters; the setting names and locations vary by editor and version, so check the current documentation for yours. A short script like the one above gives a dependable result without relying on any third-party service.

Clues that point to the cause: a gap that will not wrap suggests a no-break space; a cursor that needs an extra keypress to move past an apparently empty spot suggests a zero width character; text whose order flips when selected suggests a directional control; letters that look bold or script but fail search suggest Unicode substitute characters. Online tools were not verified for this review, and their privacy and data handling cannot be judged from how they look. Avoid pasting passwords, keys or private text into a web tool when a local editor or script will do the job.

Bottom line

Identifying non-whitespace characters is a low-risk, high-value diagnostic: it reveals what text really contains without changing it. The decision that needs care is what to do next. Normalize and strip invisible characters in identifiers, codes and comparison fields. Preserve them in prose, multilingual text and emoji, where joiners, non-joiners and directional marks do real work. And pick an explicit whitespace definition, because Unicode, JavaScript, Python and Java do not all agree.