Lets say I had a string: Roblox, and I wanted to print the string's length (6) how would I go about this?
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.
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
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?