Toolerax logoToolerax
·7 min read

How to Compare Two Texts

Two versions of a contract. Two exports of a config file. A draft and its edited return. They look identical, and something in one of them is different. Finding that difference by reading both is slow, unreliable and — as anyone who has tried it knows — almost guaranteed to miss the one that mattered. A diff does it instantly and does not get bored. This guide explains how diffs work, what they can and cannot see, and why two files that look identical sometimes are not.

Why human comparison fails

Your brain is superb at reading and terrible at proofreading. It predicts — filling in what it expects to see rather than what is actually there. That is why you can read a sentence with a missing word and not notice.

Comparing two near-identical documents is precisely the task this optimisation ruins. The single changed digit in a price, the not that was quietly removed from a clause, the swapped pair of characters — these are exactly what the brain glosses over. A diff has no such expectations. It compares character by character and reports what is there.

How a diff actually works

A diff does not simply walk both texts in step — that would fall apart the moment one line was inserted, since every subsequent line would be reported as changed.

Instead it solves a slightly clever problem: find the longest common subsequence — the largest set of lines appearing in both texts, in the same order. Anything in that set is unchanged. Everything else must have been added or removed.

This is why a diff handles insertions gracefully. Add a paragraph in the middle and it reports one addition, keeping everything after it correctly aligned. That alignment is the whole value.

It also explains something people find odd: a diff has no concept of a line being “moved.” A relocated paragraph shows up as a deletion in one place and an addition in another. There is no move operation.

Line, word or character?

Diffs work at different granularities, and choosing the right one changes how readable the result is.

  • Line diff — the standard. If a line changed at all, the whole line is marked. This is what code review uses, because code is naturally organised into lines.
  • Word diff — highlights only the changed words within a line. Much better for prose, where one line can be a whole paragraph and marking it entirely tells you nothing.
  • Character diff — the finest grain. Essential for catching a transposed digit or a single wrong letter in an ID.

A practical tip for prose: put each sentence on its own line before diffing. If a document is one long paragraph per line, a line diff will mark the entire paragraph as changed even though a single comma moved. Splitting by sentence makes the output far more useful.

Invisible characters: the phantom differences

Here is the source of the most maddening diff experience there is: two texts look absolutely identical, and the diff insists they differ. You are staring at the same line twice and being told it changed.

It has not lied to you. Something is there that you cannot see:

  • Trailing whitespace. A space or tab at the end of a line. Utterly invisible, and absolutely a difference.
  • Tabs versus spaces. Indentation that renders identically but is different bytes.
  • Line endings. The classic. Windows ends lines with \r\n, while Unix and macOS use \n. Open a file on the wrong platform and every single line shows as changed. If your diff reports a whole file as different, this is almost always why.
  • Non-breaking spaces. Copy text from a web page or Word and you frequently get U+00A0 instead of a normal space. Looks identical. Is not.
  • Smart quotes. Word silently converts " into curly quotes. Paste that into code and it breaks, while looking almost right.
  • Zero-width characters. Genuinely invisible characters with zero width, which sometimes ride along in copied text.

Good diff tools offer an “ignore whitespace” option for exactly this reason. Turn it on when you are looking for meaningful changes; turn it off when whitespace itself matters — in Python, in YAML, or when you are hunting a bug caused by an invisible character.

What people use diffs for

  • Contracts and legal documents. Confirming that the version returned to you differs only where you agreed. This is a genuinely high-stakes use, and the one where reading by eye is most dangerous.
  • Code review. Every pull request you have ever seen is a diff.
  • Configuration files. Finding what changed between a working setup and a broken one — often the fastest way to fix an outage.
  • Editorial changes. Seeing precisely what an editor altered in your draft.
  • Data verification. Confirming two exports of a list actually match.
  • Plagiarism and duplication checks, at a basic level.

Getting a cleaner diff

  • Normalise first. Make the line endings consistent before comparing, or you will drown in false positives.
  • Split prose into sentences, one per line.
  • Sort lists before diffing if order does not matter — otherwise a reordered list shows as a total rewrite.
  • Remove duplicates first when comparing lists where repeats are noise.
  • Format both sides identically. Two JSON files with the same data but different indentation will diff as completely different. Prettify both first.

Frequently asked questions

Why does my diff show every line as changed? Almost certainly mismatched line endings — one file is Windows-style, the other Unix-style.

The texts look identical but the diff disagrees. Why? An invisible character: trailing whitespace, a non-breaking space, a tab, or a zero-width character.

Can a diff compare PDFs or Word documents? Not directly — they are binary formats. Extract the text first, then diff the text.

Is it safe to paste a confidential contract into an online diff tool? Only if the tool runs entirely in your browser. If it uploads to a server, you have just sent your contract to a stranger.

Compare two texts now

Use our Text Diff Checker to see exactly what changed between two texts — it runs entirely in your browser, so confidential documents never leave your device. To tidy up lists before comparing, try Remove Duplicate Lines and the Text Sorter.

Tools mentioned in this article