September 20, 2022


A string is represented by a pair of double or single quotation marks:

local string1 = "this is string1"
local string2 = 'this is string2'

You can also use two square brackets "[[]]" to represent a "block" string:

html = [[
    <a href="">Blockman</a>

When performing arithmetic operations on a numeric string, Lua will try to convert the numeric string to a number:

print("2" + 6) -- 8.0
print("2" + "6") -- 8.0

use.. Connect two strings, such as:

local string1 = "this is string1"
local string2 = 'this is string2'
print(string1 .. string2)

Lua provides many methods to support string operation:

1. string. Upper (mainstring): all strings are converted to uppercase letters.
2. string. Lower (mainstring): all strings are converted to lowercase letters.
3. string. Gsub (mainstring, findstring, replacestring, Num): replace in a string. Mainstring is the string to be operated on, findstring is the character to be replaced, replacestring is the character to be replaced, and num replacement times (if it can be ignored, replace all).
4. string. Find (mainstring, substr, [init, [end]]): search a specified content substr in a specified target string mainstring. If a matching substring is found, the start index and end index of the substring will be returned. If it does not exist, nil will be returned.
5. string. Reverse (mainstring): String inversion.
6. string. format(…): Returns a format string similar to printf.
7. string. Char (mainstring) and string Byte (arg[, int]): char converts integer numbers to characters and connects them. Byte converts characters to integer values (you can specify a character, the first character by default).
8. string. Len (mainstring): calculates the string length.
9. string. Rep (mainstring, n): returns N copies of the string mainstring.
9. string. Gmatch (mainstring, pattern): returns an iterator function. Each time this function is called, it returns the next substring found in the string mainstring that conforms to the pattern description. If the string described by the parameter pattern is not found, the iteration function returns nil.
10. string. match(mainString, pattern, init):string. Match() only looks for the first pair in the source string mainstring The parameter init is optional. It specifies the starting point of the search process. The default value is 1. When the pairing is successful, the function will return all the capture results in the pairing expression; If no capture flag is set, the entire pairing string is returned Nil is returned when there is no successful pairing.