How to call how many chars in a string

How would i call how many chars there are in a string

example

“hello” has 5 letters

I want to call how many letters or chars there are in a set string

Thanks!

This should work for you.

Alternatively you could add a # before the variable to get its length

Alternatively, #


local str = "hello"

print(#str)
> 5

(Correct me if I’m wrong here) I’d suggest utf8.len() over the other proposed methods. Example:


lua_run_cl print( ("Ѝ"):len() )
2

lua_run_cl print( #("Ѝ") )
2

lua_run_cl print( utf8.len("Ѝ") )
1

Unless you just happen to know you’ll only use stuff like A-Z, 0-9, etc.

**-Edit-

**There appears to be a bug with GMod’s utf8.len. It’s reporting 0 length for single ASCII characters, like print( utf8.len( “a” ) ) --> 0.
This is incorrect, as it should be returning 1. Validated it using a compiled version of Lua 5.3.2.

-Edit-

utf8.len is being fixed in the next update. I’d again recommend using it over the others once the update is released.

I think this is best thanks!