Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

getting word length?

Lets say I had a string: Roblox, and I wanted to print the string's length (6) how would I go about this?

3 answers

✓ Accepted answer

There are two ways you can go about doing this

1) - You can use the string.len function, which uses a string as an argument and returns it's length. This function can either be called directly from the string as a method by doing string:len() or as a function by doing string.len(string).

local str = 'Hello world' --String to get the length of

print(str:len()) --> 11

2) - Or if you're lazy like me and like shorter routes, use the # operator. The '#' operator returns the length of any given string!

local str = 'Hello world' --String to get the length of

print(#str) --> 11

This operator also works for tables(:

local tab = {'H','e','l','l','o',' ','W','o','r','l','d'}

print(#tab) --> 11

Use string.len()

Here's an example.

print(string.len("Hey"))

This would return 3 since the string has 3 characters.

1 comment

0

Or you could use the # operator.

To print a strings length, there is a couple different ways. Both of these work:

print(string.len("String here")) --Using string.len (Length) to give the strings length.

or

print(("String here!"):len()) --Calling the "len" (Length) method.

Both of these would print "12" because the string "String here!" has 12 character, including spaces. Hope this helped :P

This discussion is preserved as it was. New answers, comments, and votes are closed.