Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

getting word length? [closed]

Asked by 9 years ago

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

Locked by BSIncorporated, Redbullusa, MessorAdmin, and chess123mate

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
Ad
Log in to vote
1
Answered by
parkderp1 105
9 years ago

Use string.len()

Here's an example.

print(string.len("Hey"))

This would return 3 since the string has 3 characters.

0
Or you could use the # operator. Vlatkovski 320 — 9y
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

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