Quirrel4.41.0

types.String.hash

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

Returns str's hash.

examples/types/string/hash-basic.nut
let squadTag = "alpha-squad"
println("squadTag.hash() == \"alpha-squad\".hash() =", squadTag.hash() == "alpha-squad".hash())
Output:
squadTag.hash() == "alpha-squad".hash() = true

Return value

A non-negative int: the FNV-1a hash the VM computed once, when str was created, and cached on the string object. Equal strings always give the same hash.

Notes

Takes no arguments; the VM reports the true arity here.

This is not the same value as math.hash(str). math.hash runs its own mixing pass, seeded independently, that folds in str's cached hash together with the string's type tag and its length - so the two functions agree on which strings are equal (equal strings give equal results from either one) but not on what the number is: "x".hash() != hash("x") in general.

Example

examples/types/string/hash.nut
let math = require("math")

// this method and math.hash() do not agree on the number itself.
println("\"abc\".hash() == math.hash(\"abc\") =", "abc".hash() == math.hash("abc"))
Output:
"abc".hash() == math.hash("abc") = false

See also

hashReturns a hash value for the given object
lenReturns the length of str in bytes.
stringclass index