types.String.hasindex
Method on every
stringReturns 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
index | number | the byte index to test |
... | any | not 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
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) = falseSee also
len | Returns the length of str in bytes. |
slice | Returns the bytes of str from start up to, but not including, end. |
| string | class index |