Compare Texts to Find Character-Level Changes

How character-level text diffs work, what they reveal that line views miss, how to choose line, word or character granularity, and when diff output stays usable.

A text diff compares two versions of a text and reports what was inserted, deleted or left unchanged. A character-level comparison narrows that report to individual characters. This matters when the change is a single letter, a punctuation mark or an invisible character, because a line-by-line view would only show it as part of a changed line.

What a character-level diff actually compares

A diff works on stored characters, not on appearance. It reads each text as a sequence of units, such as lines, words or characters. It then computes a set of insertions and deletions that turns the first sequence into the second. The output only describes those edits. Neither input text is modified.

Because a diff compares stored characters, some things that look like changes are not changes to the text. Other things that look identical are different. Several common text transformations fall on one side or the other:

  • CSS presentation: uppercase styling, mirrored or rotated display and font changes alter only how the text is drawn. If you compare the underlying text, a diff reports no difference. If you compare text copied from a rendered page, the result depends on what the browser put on the clipboard, so check it instead of assuming.
  • Character-order reversal: reversing a string rewrites the stored sequence. A diff against the original reports most characters as changed, even though the same letters are present.
  • Visual mirroring: flipping text with a display transform is presentation only. A tool labeled mirror text may instead substitute look-alike characters. The label does not tell you which method it uses, but a diff against the input does.
  • Unicode substitution: styled letters from font generators, such as mathematical bold or script letters, and look-alike letters from another script are different code points. A diff flags them as changed even when they look almost identical to the original.
  • Bidirectional display: when Arabic or Hebrew is mixed with Latin text, characters are stored in logical reading order and the display engine rearranges them on screen. A diff compares the logical order, so highlighted positions may not line up with what you see. Invisible direction-control characters can also appear as insertions.

Differences that are easy to miss

Character-level comparison is most useful where visual proofreading fails. These are common sources of differences that render identically or nearly so:

  • Normalization: an accented letter such as é can be stored as one precomposed character or as a base letter followed by a combining accent. Both render the same but compare as different.
  • Whitespace variants: a regular space versus a non-breaking space, a tab versus several spaces, or trailing spaces at the end of a line.
  • Zero-width characters: zero-width spaces, zero-width joiners and byte order marks take up no visible width but are real characters.
  • Line endings: Windows-style CRLF versus LF line endings can make every line appear changed in a line-based comparison.
  • Punctuation look-alikes: straight versus curly quotation marks, or a hyphen versus an en dash or minus sign.

If a diff reports a change you cannot see, one of these is the likely cause. To confirm which one, inspect the code points or turn on a show-invisible-characters option if your tool has one.

Line, word or character granularity: choosing the right view

The same pair of texts can be reported at different granularities. The views agree on what changed. They differ in how precisely they locate each change and how readable the result is. The comparison below uses one matched input. It is illustrative: it was written to show the notation and was not captured from a specific tool.

Original:  The colour of the sky was grey.
Revised:   The color of the sky was gray.

Line level:
- The colour of the sky was grey.
+ The color of the sky was gray.

Word level:
The [-colour-]{+color+} of the sky was [-grey.-]{+gray.+}

Character level:
The colo[-u-]r of the sky was gr[-e-]{+a+}y.
Criterion | Line level | Word level | Character level
Best suited to | Code, configuration and structured files | Prose edits, contracts and copy review | Typos, spelling variants, invisible or substituted characters
How a one-letter edit appears | Whole line marked as changed | Whole word marked as changed | Only the affected letter marked
Readability on heavy rewrites | Clear, shown as a replaced block | Mostly clear | Noisy, with fragments scattered through words
Handling of whitespace and Unicode differences | Shows which line differs, not the cause | Depends on how the tool splits words | Pinpoints the exact character

A practical approach is to start broad and then narrow down. Use a line-level view to find where the texts differ. Then switch to word or character level for those regions. If you run a character-level view across whole rewritten paragraphs, the output breaks into short matches. Those matches are technically correct but hard to follow.

A diff reports one valid set of edits, which is not necessarily how the change was actually made. Different algorithms or settings can align the same texts differently, especially around repeated words or punctuation. Two tools may therefore show different results that are equally correct.

Exact comparison versus comparing visible wording

Before you compare, decide whether invisible differences matter for your purpose:

  • Use exact comparison when character-for-character identity matters. Examples include verifying copied data, checking source code or identifiers, and detecting look-alike character substitution in names or addresses.
  • Normalize first when you only care about the visible wording, for example when reviewing prose pasted from different editors. Unifying the Unicode normalization form, collapsing whitespace and standardizing line endings removes noise. Normalization changes the text itself, so compare normalized copies and keep the originals.
  • Ignoring case or whitespace involves the same tradeoff. The output has less noise, but genuine changes of those kinds no longer appear.

Will the diff output stay readable and usable?

Highlighting is presentation. Colored or struck-through marks in a comparison view often do not survive being pasted as plain text into email, chat or a ticket. What arrives depends on both the source and the destination, so paste a sample and check it. When a diff has to be shared as plain text, use explicit markers. Plus and minus line prefixes, or bracketed insertions and deletions like those in the example above, stay readable anywhere.

Diff views that rely only on red and green are hard to read for people who cannot distinguish those colors. Web accessibility guidelines (WCAG) advise against using color as the only way to convey information. Choose views that also use symbols, strikethrough or text labels. How screen readers announce inserted and deleted text varies with the markup, the browser and the assistive technology. If a reviewer uses a screen reader, test the output with their setup or give them a plain-text summary of the changes.

Character-level markers placed inside words also break those words apart. A sentence copied from the diff view, markers included, no longer matches either version. When you need clean text, copy it from the original or the revised document, not from the comparison view.

Online comparison pages may send both texts to a server or may process them in your browser. You usually cannot tell which from the page itself. For contracts, personal data, credentials or unpublished material, use a comparison tool that runs locally. Otherwise, confirm in the service's documentation how it processes and retains submitted text.

Gaps in this guide

No specific comparison tool was tested for this guide. It therefore does not state which products offer a character-level mode, Unicode normalization or invisible-character display, or how particular applications render or copy diff output. Check your tool's documentation for those options before relying on them.