How to Find Text Differences: Line-by-Line Diff

Last updated: · 4 min read

Finding what changed between two versions of a configuration file or two drafts of a contract by eye is slow and error-prone. A text compare (diff) tool scans both texts line by line and marks lines as added, removed or unchanged. This guide explains the idea behind the difference, the longest common subsequence (LCS), what the + and - marks mean, and how the letter-case and whitespace options change the result. The output in the example is the real result of the Text Compare tool.

Formula

A: old lines (n of them), B: new lines (m of them)
1. Skip the common lines at the start and at the end.
2. L[i][j] = length of the longest common subsequence of A[i..] and B[j..]:
   if A[i] = B[j] then L[i][j] = L[i+1][j+1] + 1
   otherwise L[i][j] = max(L[i+1][j], L[i][j+1])
3. Trace back from i = 0, j = 0:
   A[i] = B[j] → "  " (same line), both i and j advance
   L[i+1][j] ≥ L[i][j+1] → "- " (removed from A), i advances
   otherwise → "+ " (added in B), j advances

The longest common subsequence is the longest set of lines that appears in both texts in the same order; the remaining lines are counted as removed or added. This is the core of classic diff tools, and GNU diff and POSIX diff use the same idea. With case sensitivity off, lines are compared in lower case; with whitespace sensitivity off, runs of spaces are reduced to a single space before comparison. Up to 2000 differing lines per side are supported.

What the inputs mean

Text A (old)
The starting text: the earlier version, the original draft or the old configuration. Each line counts as one record.
Text B (new)
The new version in which the changes were made. Removed lines are shown with -, added lines with +.
Letter case
With “Ignore”, Hello and hello count as the same line. For code and identifiers, “Treat as different” is safer.
Whitespace
With “Ignore”, extra spaces at the line end or between words do not change the result. In indented code, keep “Treat as different”.

Worked example: Comparing two three-line texts

  1. Text A: one, two, three. Text B: one, 2, three, four. Letter case and whitespace are both set to “Treat as different”.
  2. The common first line “one” is skipped; the middle lines two and 2 are compared and the trace finds that “three” is shared.
  3. “two” exists only in A, so it is marked with - as a removed line.
  4. “2” exists only in B, so it is marked with + as an added line; “three” is a common line, and “four” at the end is marked with + as added.
  5. The summary is counted: 2 added, 1 removed and 2 unchanged lines.

Result: Tool output: Added lines: 2, Removed lines: 1, Unchanged lines: 2; then the lines “ one”, “- two”, “+ 2”, “ three” and “+ four” follow.

🔍 Text Compare (Diff)

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Lines beginning with two spaces are present in both texts; lines beginning with - exist only in the old text, and lines beginning with + only in the new one. A change with many unchanged lines is usually a small edit. If the added and removed counts are close, the same lines may have been moved, which shows up as one deletion and one insertion.

Common mistakes

  • Ignoring letter case. A change from Key to key is a real change in configuration keys.
  • Mistaking line-ending differences for changes. Windows (CRLF) and Unix (LF) line endings are treated alike by this tool, but other programs may show them as differences.
  • Misjudging whitespace. In indentation-based languages such as Python or YAML, a whitespace change can break the code; the Ignore option hides that change.
  • Expecting word-level differences. The tool works line by line; if one word changes, the whole line appears as removed and added.
  • Comparing very large texts without splitting them. Up to 2000 differing lines per side are supported; split larger files into parts.

Frequently asked questions

Why are two lines marked when I changed only one?

A diff compares whole lines. The changed line appears once as removed (-) in its old form and once as added (+) in its new form.

When is it right to ignore whitespace?

It suits free-form text such as prose or Markdown, where spacing is not meaningful. It is usually wrong for code and indented structures.

What do lines starting with two spaces mean?

They are identical in both texts. The tool shows them with two leading spaces, the standard way a diff marks an unchanged line.

Are my texts sent to a server?

The comparison runs in your browser on the page. Still, check texts that contain secrets before pasting them anywhere.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice