Game Development Reference
In-Depth Information
Chapter 5
Strings
Hopefully you survived the previous chapter, on Lua math. The whole idea of these first few chapters
is to introduce you to the fact that you can create your logic in Lua and then use any framework
to add the graphics. So, many of the functions that we shall create in these chapters can be used
universally across most of the frameworks without many changes. In this chapter we shall explore
the topic of strings.
What Is a String?
A string is one of the many variable types available in Lua. It can hold alphanumeric characters of
arbitrary lengths. It cannot have multiple dimensions like a table can. Lua automatically converts
strings into numbers if the strings are used in any form of arithmetic operation. This process is called
coercion , and it is automatic.
You have seen strings in many forms earlier, the simplest being
print("hello world!")
The string in this case is "hello world!" . It is mainly characterized by enclosing the alphanumeric
characters within double or single quotes.
In this case, since the "hello world" is not assigned to a variable but used directly, it is called a
constant string .
Most of the string functions are used to manipulate text. The functions available for use in Lua are
described in the following sections.
Note Many of the functions available are also provided within a namespace. So, you can also use the
functions in an object-oriented style. For example string.byte(s,i) can be represented as s:byte(i) .
65
 
 
Search WWH ::




Custom Search