compare
Builtin index · Collections & strings
result = compare(a, b, [ignore_case])
Input: a, b, ignore_case · Output: result
What it is
a and b are Strs; optional ignore_case (bool, default false).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = compare(a, b, [ignore_case]) | a Stra and b are Strs.b Stra and b are Strs.ignore_case bool, optionaloptional ignore_case (bool, default false). | result number | Returns a number: −1, 0 or 1, so it can drive a sort directly. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
s = "Hello, Qu World!"
print(mid(s, 7, 2)) # "Qu"
print(left(s, 5)) # "Hello"
print(right(s, 6)) # "World!"
print(reverse("abc")) # "cba"
print(lower("SHOUT")) # "shout"
print(ucase("shout")) # "SHOUT"
print(lcase("SHOUT")) # "shout"
print(toupper("mix")) # "MIX"
print(tolower("MIX")) # "mix"
print(capitalize("ada lovelace")) # "Ada lovelace"
print(ltrim(" padded")) # "padded"
print(rtrim("padded ") + "|") # "padded|"
print(pad_right("ab", 5, ".")) # "ab..."
print(compare("apple", "banana")) # -1
print(asc("A")) # 65
output
Qu
Hello
World!
cba
shout
SHOUT
shout
MIX
mix
Ada lovelace
padded
padded|
ab...
-1
... 1 more linesfrom collections-strings
How it works
compare(a, b, [ignore_case]) — -1, 0 or 1, so it can drive a sort directly. Case-insensitive comparison is the reason this exists: lower(a) == lower(b) allocates two strings to answer a question that needs none.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.