-
Notifications
You must be signed in to change notification settings - Fork 11
String Indexing
BlackBulletIV edited this page May 9, 2011
·
4 revisions
Strong allows for two types of indexing. These are documented here.
Indexing with brackets ([]
) allows you to get a single character of a string by provided its index. It's nearly the same as using s:sub(i, i)
but it returns nil if the index is out of range.
Example
s = "Hello"
print(s[1]) -- "H"
print(s[3]) -- "l"
print(s[-1]) -- "o"
print(s[20]) -- nil
For more powerful indexing, you can actually call the strings, as in s(i, j)
. There are three things you can do with it:
- If you provide only
i
, andi
is a number, it will be the same ass[i]
. - If you provide
i
andj
, andi
is a number, then it will be the sames:sub(i, j)
, with the exception that it will return nil ifi
is out of range. - Finally, if
i
is a string it will be the same ass:match(i, j)
.
Example
s = "Hello"
print(s(2)) -- "e"
print(s(2, 4)) -- "ell"
print(s("ell")) -- "ell"
print(s("nothere")) -- nil
print(s("l", 4)) -- "l"
print(s("l", 5)) -- nil