last_index_of
Builtin index · Collections & strings
result = last_index_of(s, sub)
Input: s, sub · Output: result
What it is
s and sub are Strs.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = last_index_of(s, sub) | s Strnot documented yetsub Strnot documented yet | number —0-based character position of the last occurrence, searching from the end | Returns a number (0-based character position of the last occurrence, searching from the end) or none if absent — so a miss cannot be mistaken for a position. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
path = "report_final_v2.txt"
print(ends_with(path, ".txt")) # true
print(index_of(path, "_")) # 6
print(last_index_of(path, "_")) # 12
print(insert("helloworld", 5, ", ")) # "hello, world"
print(remove("hello, world", ", ")) # "helloworld"
line = "key: value"
print(after(line, ": ")) # "value"
print(before(line, ": ")) # "key"
full = "a/b/c.txt"
print(after_last(full, "/")) # "c.txt"
print(before_last(full, "/")) # "a/b"
output
true 6 12 hello, world helloworld value key c.txt a/b
from collections-strings
How it works
last_index_of(s, sub) — like index_of from the other end, and like it returns none rather than -1 when absent, so a missing result cannot be mistaken for a position.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.