string.len()
string.len() is a string function that returns the length of the given string. This will return the length of the string including any and all characters. You can either use this as a function;
local str = "Hello, i'm bob"
print(string.len(str))
Or, you can use this as a method of the specified string;
local str = "Hello, i'm bob"
print(str:len())
A shorter alternative to this is using the # operator, which will also return the length of any given string, as well as tables!
local str = "Hello, i'm bob"
print(#str)
string.sub()
string.sub() is a function that returns substrings of the given string. Substrings are portion of a string. You can determine what portion of said string will be returned with the arguments of this function.
There are a few ways you can use string.sub.
- string.sub(string,start,stop)
Where string would be the string you're getting the substring from, start would be the start of the substring, what place in the string argument.. 1 is the first letter, 3 is the second letter.. you get the point.. and stop would be the end of the substring.
local str = "Hello, world"
local substring = string.sub(str,3,6)
print(substring)
Same rules as the previous apply, just now it will get any and all characters after the given start place
local str = "Hello, world"
local substring = string.sub(str,8)
print(substring)
Note: As for the start, and stop arguments.. negative numbers count backwards in the string(: -1 would be the last letter, -2 would be the second to last letter, etc..
1 comment
string.len is old Lua 5.0 terminology, the standard method is #string