#!/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/lua 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)The preceding produces the following output:
slitt@mydesk:~$ ./test.luaNotice the following:
Student Peter Piper has a test average of 87.6%.
slitt@mydesk:~$
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.
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.lua
rltrim=>Here I stand<
slitt@mydesk:~$
[ Troubleshooters.com | Code Corner | Email Steve Litt ]
Copyright
(C) 2011 by Steve Litt -- Legal