Can "string.lend" be used in anything other than counting the number of words and spaces in a sentence?

So, as mentioned above, I would like to know if string.len has any more functionality besides saying the number of spaces and letters?

1 Like

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.

2 Likes

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
2 Likes

sorry I didn’t read this now, thanks for understanding better.