Quirrel4.41.0

types.String.hasindex

Method on every string
pure string.hasindex(index: number, ...): any

Returns whether index is a valid byte position in str.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

indexnumberthe byte index to test
...anynot a real parameter; see Notes repeats

Return value

true when 0 <= index < str.len(), false otherwise. Unlike types.String.slice, a negative index is not counted from the end - it is out of range, so it gives false.

Notes

Takes exactly one argument, not the variadic tail the VM's dump implies (it shows hasindex(arg1: number, ...) because this binding carries no declaration string). A second argument is silently ignored.

Example

examples/types/string/hasindex.nut
println("\"abc\".hasindex(0) =", "abc".hasindex(0))
println("\"abc\".hasindex(2) =", "abc".hasindex(2))
println("\"abc\".hasindex(3) =", "abc".hasindex(3))    // one past the end

// negative index is simply out of range here, unlike slice.
println("\"abc\".hasindex(-1) =", "abc".hasindex(-1))
Output:
"abc".hasindex(0) = true
"abc".hasindex(2) = true
"abc".hasindex(3) = false
"abc".hasindex(-1) = false

See also

lenReturns the length of str in bytes.
sliceReturns the bytes of str from start up to, but not including, end.
stringclass index