How to call how many chars in a string

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


“hello” has 5 letters

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


This should work for you.

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

Alternatively, #

local str = "hello"

> 5

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

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

lua_run_cl print( #("Ѝ") )

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

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


**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.


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!