#!/usr/bin/luaYou can build a string whose characters are all the same with string.rep() as shown:
local fname = "Barack"
local lname = "Obama"
local wholename = fname .. " " .. lname
print(wholename) --Prints "Barack Obama"
#!/usr/bin/luaThe preceding prints a line of equal signs 20 long:
local fname = string.rep('=', 20);
print(fname)
slitt@mydesk:~$ ./test.lua
====================
slitt@mydesk:~$
#!/usr/bin/luaThe preceding produces the following output:
local fname="Peter"
local lname="Piper"
local avg=87.567
local str = string.format("Student %s %s has a test average of %4.1f%%.", fname, lname, avg);
print(str)
slitt@mydesk:~$ ./test.luaNotice the following:
Student Peter Piper has a test average of 87.6%.
slitt@mydesk:~$
#!/usr/bin/luaThe preceding produces the following output:
local str = string.format("%-10s %-10s %8s", "Lastname", "Firstname", "Amount");
print(str)
local str = string.format("%-10s %-10s %8s", "--------", "---------", "------");
print(str)
local str = string.format("%-10s, %-10s contributed $ %8.2f", "Anderson", "Al", "1200");
print(str)
local str = string.format("%-10s, %-10s contributed $ %8.2f", "Foster", "Fred", "32.67");
print(str)
local str = string.format("%-10s, %-10s contributed $ %8.2f", "Jones", "John", "400");
print(str)
local str = string.format("%-10s, %-10s contributed $ %8.2f", "Smith", "Sam", "900");
print(str)
slitt@mydesk:~$ ./test.luaWe gave every last name and every first name a width of 10, and by making that number negative we left justified it instead of the default right justification. For the amounts, which should always be right justifed, we made it right justified by having a positive width. It was easy to make the headers match up to the line items because we used the same widths.
Lastname Firstname Amount
-------- --------- ------
Anderson , Al contributed $ 1200.00
Foster , Fred contributed $ 32.67
Jones , John contributed $ 400.00
Smith , Sam contributed $ 900.00
slitt@mydesk:~$
string.sub(strng, index_start [, index_end])Where strng is the string to be cut up, index_start is the index of the first character to be returned, and index_end is the index of the last character to be returned. If index_end is absent, everything between the character at index_start and the end of the larger string is returned.
#!/usr/bin/luaThe preceding program prints the following output:
print(string.sub("123456789", 1)) --should print original string
print(string.sub("123456789", 1, -1)) --should print original string
print(string.sub("123456789", 4, 7)) --should print 4567
print(string.sub("123456789", 1, 4)) --should print the 1st 4 chars
print(string.sub("123456789", -4, -1)) --should print the last 4 chars
print(string.sub("123456789", 2, -2)) --should remove the first and last char
print(string.sub("123456789", 2)) --should remove the first char
print(string.sub("123456789", 1, -2)) --should remove the last char
print(string.sub("123456789", -2)) --should print only the last 2 chars
print(string.sub("123456789", 1, 2)) --should print only the first 2 chars
slitt@mydesk:~$ ./test.luaUMENU menu definition files are a case in point. The first character is a key, the second character can be anything and is just for humans and ignored by the program, and from the third character on is the value. Here's how you do that:
123456789
123456789
4567
1234
6789
2345678
23456789
12345678
89
12
slitt@mydesk:~$
#!/usr/bin/luaThe preceding code prints the following, as expected:
local strng = "T_Kt to MPH converter"
local key = string.sub(strng, 1, 1)
local value = string.sub(strng, 3)
print(strng)
print(key)
print(value)
slitt@mydesk:~$ ./test.luaThe string.sub() function can be combined with other string handling functions to do quite powerful things.
T_Kt to MPH converter
T
Kt to MPH converter
slitt@mydesk:~$
| Syntax |
Returns |
Does |
| string.find(s, pattern [, init [, plain]]) | index of start of match |
By returning the index of the found match this gives you a great way to carve up string. |
| string.match(s, pattern [, init]) | capture | Returns the capture (instance of the found pattern), or nil
if none were found. Quick way of getting a small string out of a big
one, or of detecting whether the pattern exists. |
| string.gmatch(s, pattern)
|
iterator | The iterator repeatedly finds the next instance of the found pattern (capture), so it can be used in a generic while loop. |
#!/usr/bin/luaThe preceding produces the following output:
local strng = " Here I stand "
local rtrim = string.match(strng, ".*%S")
local rltrim = string.match(ltrim, "%S.*")
print(string.format("rltrim=>%s<", rltrim))
slitt@mydesk:~$ ./test.luaLet's examine the preceding carefully. On the right trim, we look for everything until the last non-space (%S is a non-space printing character, %s is a space character). But why did it take until the last non-space instead of until the first one? It's because Lua's string package's default behavior is greedy matching, meaning match all the way until the last instance of what you're looking for. So in the pattern ".*%S", it takes all characters until the last non-space. If you wanted to take them only until the first non-space, you'd use a minus sign instead of an asterisk, so it would look like this: ".-%S". That's called non-greedy matching. This will be discussed in on the "Lua Regex" page.
rltrim=>Here I stand<
slitt@mydesk:~$
#!/usr/bin/luaThe inside function is evaluated first, and it trims spaces off the left. I think that's probably quicker. The inside function pass its result as a function return, and the outside function works on that. The output follows:
local strng = " Here I stand "
local rltrim = string.match(string.match(strng, "%S.*"), ".*%S")
print(string.format("rltrim=>%s<", rltrim))
slitt@mydesk:~$ ./test.luaAs a final example, consider the following code to take a typical config file line with a key, an equal sign, and a value, and extracts the key and value from it:
rltrim=>Here I stand<
slitt@mydesk:~$
#!/usr/bin/luaThat's some ugly code folks. In the Lua "Regex" lesson you'll learn easier, neater and more concise ways to do things like this. The following is the output of the preceding:
local strng = " fullname = Mary Theresa Colleen Maureen McAllister "
local key = string.match(strng, "^.-=")
key = string.sub(key, 1, -2)
key = string.match(key, ".*%S")
key = string.match(key, "%S.*")
local value = string.match(strng, "=.*$")
value = string.match(value, "[^=%s].*")
value = string.match(value, ".*%S")
print(string.format("strng=>%s<", strng))
print(string.format("key=>%s<", key ))
print(string.format("value=>%s<", value))
slitt@mydesk:~$ ./test.luaNotice that both the key and the value have been trimmed of blanks on both sides.
strng=> fullname = Mary Theresa Colleen Maureen McAllister <
key=>fullname<
value=>Mary Theresa Colleen Maureen McAllister<
slitt@mydesk:~$
[ Troubleshooters.com| Code Corner | Email Steve Litt ]
Copyright
(C) 2011 by Steve Litt --Legal