commonPrefix

commonPrefix, function

def const pure commonPrefix(a: text, b: text): number

Returns the number of characters that appear, identical, at the start of both a and b. It is the length of the longest text common such that startsWith(a, common) and startsWith(b, common).

Examples

show table "Common prefixes" with
  commonPrefix("ABCD", "ABEF") as "ABCD vs ABEF"
  commonPrefix("abcd", "ABCD") as "abcd vs ABCD"
  commonPrefix("ABCD", "ABC") as "ABCD vs ABC"

Output:

ABCD vs ABEF abcd vs ABCD ABCD vs ABC
2 0 3

Remarks

You can retrieve the common prefix (and not just its length) with substr(a, 0, commonPrefix(a, b)). This function is usable in const contexts.

See also

User Contributed Notes
0 notes + add a note