How Text Diff and Comparison Tools Work

difftext

A diff tool shows what changed between two versions of text by marking lines as added, removed or unchanged. The interesting part is how it decides which lines match.

The core problem

The goal is to find the longest common subsequence, or LCS, of two lists of lines. Once shared lines are identified, everything before them is a removal, and everything after them is an addition.

Line-based versus word-based

  • Line-based diff is fast and readable for code and prose.
  • Word-based or character-based diff is finer grained and better for short edits inside a sentence.

Diff in code

Python:

import difflib
a = ['hello', 'world']
b = ['hello', 'there']
print('\n'.join(difflib.unified_diff(a, b)))

Common pitfalls

  • Whitespace changes can look like big edits even when the text is identical.
  • A moved block is usually reported as one removal plus one addition.
  • Very large inputs need a diff that stays fast and memory-friendly.

Frequently asked questions

  • What do the colors mean? Green is added, red is removed, and uncolored lines are unchanged.
  • Is my text uploaded? No. A browser-based diff runs locally.

Need to compare two blocks of text? Try the free Text Diff Compare — it highlights changes locally.