Skip to content

text.compare

how different two strings are, the closest match from a list, and line-by-line differences

How different two strings are, and what changed between two texts. The edit distance between two strings is the fewest single-character changes that turn one into the other, where a change is adding a character, removing one, replacing one, or swapping two that are next to each other. “form” and “from” are 1 apart, and “kitten” and “sitting” 3, so it’s a good measure of how likely one is a typo of the other.

Tags: searching

  • distance the edit distance: how many single-character changes turn a into b
  • hamming how many positions two equal-length strings differ at, the Hamming distance
  • similarity how alike two strings are, from 1 (identical) to 0 (nothing in common)
  • closest the option with the smallest edit distance to s, ignoring upper and lower case, or nil for an empty list
  • diff what changed from text a to b, line by line, or nil if nothing did
distance(a: str, b: str) -> int

the edit distance: how many single-character changes turn a into b

1
distance("kitten", "sitting")3
2
distance("form", "from")1
Example outputCtrl+Enter to run

See also similarity, closest

hamming(a: str, b: str) -> int / hamming(a: bytes, b: bytes) -> int

how many positions two equal-length strings differ at, the Hamming distance

Bytes count differing bits instead, the measure for guessing a repeating XOR key’s length

1
hamming("karolin", "kathrin")3
2
hamming(b"this is a test", b"wokka wokka!!!")37
Example outputCtrl+Enter to run

See also distance, xor

similarity(a: str, b: str) -> float

how alike two strings are, from 1 (identical) to 0 (nothing in common)

It’s the edit distance as a share of the longer string’s length, taken from 1

1
similarity("color", "colour")0.833333
Example outputCtrl+Enter to run

See also distance

closest(s: str, options: list) -> any

the option with the smallest edit distance to s, ignoring upper and lower case, or nil for an empty list

1
"aple".closest(["apple", "maple", "ape"])"apple"
Example outputCtrl+Enter to run

See also distance

diff(a: str, b: str, *, plain: bool = false, bytes: bool = false, table: bool = false, base?: bool|int) -> any

what changed from text a to b, line by line, or nil if nothing did

Changes come in hunks headed @@ with their line numbers, with 3 unchanged lines around them. Removed lines start with - and added lines with +, colored on a terminal with the changed words picked out. With the table flag, it is instead a table with a row per changed line, each with both sides’ line numbers and lines, a removed line next to the one that replaced it and nil on a side that has none

1
diff("a\nb\nc", "a\nc\nd")
@@ -1,3 +1,3 @@ a - b c + d
2
diff("a\nb\nc", "a\nB\nc\nd", table: true)table([{line_a: 2, line_b: 2, old: "b", new: "B"}, {line_a: nil, line_b: 4, old: nil, new: "d"}])
Example outputCtrl+Enter to run

As a command:

  • -p, --plain no colors, even on a terminal
  • -b, --bytes the command reads the files as bytes, for the bytes diff
  • -t, --table a table of changed lines instead of hunks
  • -B, --base for the bytes diff, the address the first byte is at
diff notes.txt notes2.txt
# → diff(read_file("notes.txt"), read_file("notes2.txt"))

See also distance

1
# fix a typo
2
"recieve".closest(["deceive", "receive", "recipe"])"receive"
3
# how alike two spellings are
4
similarity("color", "colour").percent(0)"83%"
5
# what changed
6
diff("a\nb\nc", "a\nB\nc\nd")
@@ -1,3 +1,4 @@ a - b + B c + d
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.