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

What is :len() and :sub()?

Asked by 9 years ago
for i = 1,text:len() do
label.Text = text:sub(1,i)
wait(0.1)
end

I usually don't work with for loops, so I searched around wiki and there was nothing that really explained :sub() very well,

I'm finishing a script for a friend, and it's a simple 10 line script, except there's nothing saying anything about :len() on wiki or in the script, so I'm wondering if any of you know.

0
string.len is old Lua 5.0 terminology, the standard method is #string 1waffle1 2908 — 9y

2 answers

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

string.len()

string.len() is a string function that returns the length of the given string. This will return the length of the string including any and all characters. You can either use this as a function;

local str = "Hello, i'm bob"

print(string.len(str)) --> 14

Or, you can use this as a method of the specified string;

local str = "Hello, i'm bob"

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

A shorter alternative to this is using the # operator, which will also return the length of any given string, as well as tables!

local str = "Hello, i'm bob"

print(#str) --> 14

string.sub()

string.sub() is a function that returns substrings of the given string. Substrings are portion of a string. You can determine what portion of said string will be returned with the arguments of this function.

There are a few ways you can use string.sub.

  • string.sub(string,start,stop)

Where string would be the string you're getting the substring from, start would be the start of the substring, what place in the string argument.. 1 is the first letter, 3 is the second letter.. you get the point.. and stop would be the end of the substring.

local str = "Hello, world"
local substring = string.sub(str,3,6)

print(substring) --> llo,
  • string.sub(string,start)

Same rules as the previous apply, just now it will get any and all characters after the given start place

local str = "Hello, world"
local substring = string.sub(str,8)

print(substring) --> world

Note: As for the start, and stop arguments.. negative numbers count backwards in the string(: -1 would be the last letter, -2 would be the second to last letter, etc..

Ad
Log in to vote
0
Answered by 9 years ago

:len() is the total amount of Characters in a string example "Dog" has a :() len of 3, now sub is like a cutoff example

Example = "apple"
print(Example:sub(2,5))

This would print "pple" because it only goes to the beginning set variable and the end

0
`sub` stands for substring, and `len` for length. Example:sub(2) goes from the 2nd character to the end of the string. http://lua-users.org/wiki/StringLibraryTutorial adark 5487 — 9y

Answer this question