Quirrel4.41.0

types.String.len

Method on every string
pure string.len(): any

Returns the length of str in bytes.

Return value

The number of bytes in str. A Quirrel string is a byte buffer, not a sequence of code points: a 2-byte UTF-8 character counts as 2, not 1.

Notes

Takes no arguments; the VM reports the true arity here (len(): any), so there is nothing for a page to override.

Example

examples/types/string/len.nut
println("\"hello\".len() =", "hello".len())

// a Quirrel string is a byte buffer: this 2-byte UTF-8 character counts as 2.
let mb = "\xD0\x9F"
println("mb.len() =", mb.len())

try { "hello".len(1) } catch (e) println("\"hello\".len(1) throws:", e)
Output:
"hello".len() = 5
mb.len() = 2
"hello".len(1) throws: wrong number of parameters passed to native closure 'len' (2 passed, 1 required)

See also

sliceReturns the bytes of str from start up to, but not including, end.
hasindexReturns whether index is a valid byte position in str.
stringclass index