Qu

index_of

Builtin index · Collections & strings

result = 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.

CodeInputOutputDescription
result = index_of(s, sub)
s Strnot documented yet
sub Strnot documented yet
number —0-based character position of the first occurrence
Returns a number (0-based character position of the first occurrence) or none if sub is absent.

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

indexof(collection, value) — index of the first element equal to value, or Value::Nothing if absent. Nothing (not -1) is Qu's existing "absent" marker (see how ??/ Expr::Coalesce already treats it specially) — composes directly with that operator: indexof(xs, v) ?? -1 for scripts that do want a sentinel index. index_of is accepted as well: it is the spelling most people reach for, and having only one of the two is a guessing game with no payoff.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.