Understanding Lua Strings
In this article, we will explore Lua strings, their properties, operations, and usage with examples.
Strings are an important data type in programming that represent a sequence of characters. They are widely used for storing and manipulating textual data.
In Lua, strings can be created using single quotes(‘), double quotes(“) or square brackets (string is placed between [[ and ]]).
Lua Strings Properties
Lua strings are immutable, meaning they cannot be changed after creation.
They can be concatenated, sliced, and compared, but any modification will create a new string object.
Strings in Lua have a maximum length of 2^31 – 1 bytes and are encoded in UTF-8 by default. However, it is possible to use other encodings by manipulating the string bytes manually.
Below example demonstrates the initialization of strings in Lua:
Escape characters are special characters that allow us to include characters in a string that would otherwise be difficult or impossible to type directly.
These characters are represented using a backslash (\) followed by a special code.
The following table displays the available escape characters in Lua:
|\[||Left square bracket|
|\]||Right square bracket|
The following example implements the escape characters in Lua:
Lua String Methods
Lua supports the following string methods:
Returns the argument str with all its letters in uppercase.
Here is a brief example that uses string.upper in Lua:
Returns the string passed as argument with all the alphabetical characters in lowercase.
Below example makes use of string.lower function in Lua:
string.sub(str, i, j)
This method is used to extract a substring from a string. It takes two or three arguments: the string(str) from which to extract the substring, the starting index i (inclusive) of the substring, and the optional ending index j (inclusive) of the substring.
The following example demonstrates the string.sub method in Lua:
Replaces the occurrences of a pattern in a string with a new string or value.
Here, var is the string variable on which the operation is performed, find is the string to be replaced, replace is the string to be added in place of find while [n] is optional where the maximum number of replacements is to be specified.
If it is not specified, all occurrences are replaced.
Let’s explore the string.gsub() method using an example:
This function finds the first occurrence of a pattern in a string and returns its starting and ending indexes.
Here, var is the string variable on which the operation is performed and find is the string that is being searched.
The argument named as first specifies the starting index of the search and is optional. Moreover, plain is a boolean value that specifies if the find value is a pattern or plain text. If this value is true, the find value is plain text. It is also optional.
For a clear understanding of string.find(), let’s take a look at an example:
Used to reverse the order of characters in a string passed as an argument.
The following example demonstrates the string.reverse() method in Lua:
Returns a formatted string. Here str is the string that has placeholders for values while … represents values to be inserted into the placeholders.
Placeholders are specified below:
|%d||inserts an integer value.|
|%f||inserts a floating-point value.|
|%s||inserts a string value.|
To simplify the concept, let’s preview an example:
This is a function in Lua that takes any number of integer arguments and returns a string consisting of the characters with the corresponding Unicode values.
Following example showcases the implementation of the string.char() method:
It returns the ASCII value(s) of the character(s) at the specified position(s) in a string, given a string and an optional index.
In the absence of an index, the function returns the ASCII values of all characters.
Below example illustrates the string.byte() method in Lua:
Returns the length of a string passed as an argument.
This is a brief example that covers the string.len() method in Lua:
Lua string.rep(str, n))
A string(str) and a count(n) are passed as arguments, and a new string is returned consisting of the original string repeated count times.
Following example provides an overview of string.rep() method in Lua:
Lua string.match(str, find)
Used to search a string(str) for a pattern(find) and returns the matching substring.
It takes two arguments:
First is the string to be searched, and the second is a pattern to match against.
Pattern matching is done using a combination of literal characters and special characters that have a specific meaning in Lua’s pattern matching syntax.
This example makes use of the string.match method in Lua: