Strings and the String library.
Exercises, examples and other material relating to training module U104. This topic is presented on public courses Learning to program in Lua
, Lua Programming
, Lua Programming
|Articles and tips on this subject||updated|
|4274||String formatting in Lua - string.format as a wrapper for sprintf|
Lua is a small language - and so for some elements of its functionallity it uses wrappers around underlying C functions rather than re-implementing and duplicating logic. A good example is the string.format function which in essence is a wrapper around sprintf.
thing = string.format("> ...
|2500||Dynamically formatting your results (Lua)|
Scenario: You're producing a short (text) table of results and you want to keep it reasonably small - not a great deal of white space, but the data in some of your columns differs dramatically from one set of results to another. How can you do it?
Discussion: You can use your traditional "printf" ...
|2357||Checking if the user has entered a number (Lua)|
In Lua, there's a difference between 0 and nil - and that means that you can easily code to ask your user to enter a number, testing the result and allowing him to enter zero as a legitimate value. That may sound like it's no big deal, but you don't want a loop to exit on a premature 0.
Here's code ...
|2348||Finding literals rather than patterns in Lua|
Lua's string.find looks for a PATTERN ... it's not as big an sophisticated as a regular expression match (but then it's got nothing like as big a footprint!), but neither is it a literal match. So if I want to search a string for - literally - a ".", I need to protect it. I can do this either with ...
|1744||Lua examples, Lua Courses|
Programming training is much more effective if the delegates are shown how a particular program is written, rather than just the final result ... show them just the final result and they'll ask "but how did you come up with that?"
So whenever possible, I write new examples in front of my delegates - ...
Examples from our training material
|ee|| Excercise to print out exchange rate table|
|gimme|| Reading from the keyboard and string basics|
|kiddos|| Spread money gift around a group of children|
|kids|| String formatting|
|mlx|| Single and multiline strings|
|moopark|| Answer to excercise - Theme park takings|
|qbs|| backslash specials in character strings and other basics|
|s2|| Finding literal strings|
|sf|| string.format |
|splitter|| Using string.find and string.sub to split|
|stem|| Basic String functionality|
|three|| Wait until a NUMBER has been entered|
|two|| Checking a NUMBER has been entered|
|ul|| Use of string.rep|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Double and single quotes, square brackets and backslashes.
String concat, length, etc
Formatting numbers in to strings.
String Conversion Functions.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.