So, as mentioned above, I would like to know if string.len has any more functionality besides saying the number of spaces and letters?
No, it cannot, all it does is count the words/spaces in a sentence.
Please use the Roblox Wikia instead of posting about this next time.
The title is meant to be string.len() not string.lend() by the way, string.lend() doesnât exist.
I assume you made a typo of string.len
string.len counts the length of the string (in ANSI encoding, UTF-8 characters is counted as 2).
What did you expect string.len to do other than counting the length of a string?
To be technical here.
Itâs not used to find number of characters in a sentence but rather a string, which can have spaces, words, numbers.
Besides that, # is way better since it also counts the number of list/table components (not dictionary tho).
It actually just counts the amount of bytes in the string, and it doesnât have anything to do with encoding.
By the way: if you have non-alphanumeric characters in your string, you can use the utf8 libraryâs len method to count the real number of characters (codepoints) in your string rather than it being bloated due to some characters taking up more than one byte.
Take for example the character for infinity â. This takes up three bytes. Not that youâd particularly need this metric or anything, but if you wanted to count how many bytes it was:
print(string.len(utf8.char(8734))) -- 3
If we attach this character to the string âfooâ, then there are 6 bytes in this string even though you see it as 4 characters. string.len will return 6. Each of the characters from âfooâ take up one byte plus an additional three from the infinity character. However, if we use utf8âs len, it factors that in and deals with codepoints rather than bytes - therefore, len returns 4.
local foo = "fooâ"
print(string.len(foo)) -- 6
print(utf8.len(foo)) -- 4
Heads up for those with typewriter systems: if youâve got symbols that appear as boxes, youâre running into the bytes not being written to the string, thus an invalid character. You can iterate over a string using utf8.codes, a generator for a for loop, which will properly write out your characters.
local foo = "fooâ"
for _, codepoint in utf8.codes(foo) do
print(utf8.char(codepoint))
end
sorry I didnât read this now, thanks for understanding better.