Basic Methods for operating strings in Lua
This article describes the basic methods for operating strings in Lua. It is the basic knowledge of Lua beginners. For more information, see
A string is a character sequence and a control character. A string can be initialized in three forms, including:
Character between single quotes
Double quotation marks
[] Characters [[and]
An example of the above three forms is as follows.
The Code is as follows:
String1 = "Lua"
Print ("" String 1 is "", string1)
String2 = 'tutorial'
Print ("String 2 is", string2)
String3 = [["Lua Tutorial"]
Print ("String 3 is", string3)
When we run the above program, we will get the following output.
The Code is as follows:
"String 1" is Lua
String 2 is Tutorial
String 3 is "Lua Tutorial"
Use strings in escape character sequences to change the normal interpretation of characters. For example, double quotation marks ("") are printed. In the above example, we have used the "escape sequence, which is listed in the following table.
String operation
Lua supports string operation strings:
Now let's look at several examples to see the behavior of these string operation functions.
Case operations
Sample Code used to manipulate String Conversion in upper and lower case is as follows.
The Code is as follows:
String1 = "Lua ";
Print (string. upper (string1 ))
Print (string. lower (string1 ))
When we run the above program, we will get the following output.
The Code is as follows:
LUA
Lua
Replace a substring
The following is an example code of the number of occurrences of replacing a string with another one.
The Code is as follows:
String = "Lua Tutorial"
-- Replacing strings
Newstring = string. gsub (string, "Tutorial", "Language ")
Print ("The new string is", newstring)
When we run the above program, we will get the following output.
The Code is as follows:
The new string is Lua Language
Search and reverse
The following is an example code used to search for string indexes and reverse strings.
The Code is as follows:
String = "Lua Tutorial"
-- Replacing strings
Print (string. find (string, "Tutorial "))
ReversedString = string. reverse (string)
Print ("The new string is", reversedString)
When we run the above program, we will get the following output.
The Code is as follows:
5 12
The new string is lairotuT auL
Format a string
In many programming scenarios, we may need to print strings in a formatted manner. You can use strings. Format the function to format the output, as shown in.
The Code is as follows:
String1 = "Lua"
String2 = "Tutorial"
Number1 = 10
Number2 = 20
-- Basic string formatting
Print (string. format ("Basic formatting % s", string1, string2 ))
-- Date formatting
Date = 2; month = 1; year = 2014
Print (string. format ("Date formatting % 02d/% 02d/% 03d", date, month, year ))
-- Decimal formatting
Print (string. format ("%. 4f", 1/3 ))
When we run the above program, we will get the following output.
The Code is as follows:
Basic formatting Lua Tutorial
Date formatting 02/01/2014
0.3333 [code]
Character and byte Representation
Character and byte represents a sample code, which is used to convert strings to internal representation, and vice versa.
[Code] -- Byte conversion
-- First character
Print (string. byte ("Lua "))
-- Third character
Print (string. byte ("Lua", 3 ))
-- First character from last
Print (string. byte ("Lua",-1 ))
-- Second character
Print (string. byte ("Lua", 2 ))
-- Second character from last
Print (string. byte ("Lua",-2 ))
-- Internal Numeric ASCII Conversion
Print (string. char (97 ))
When we run the above program, we will get the following output.
The Code is as follows:
76
97
97
117
117
A
Other common functions
Common string operations, including string connection, finding a string, and repeating the same string for multiple times. These operations are described in the following example.
The Code is as follows:
String1 = "Lua"
String2 = "Tutorial"
-- String Concatenations using ..
Print ("Concatenated string", string1.. string2)
-- Length of string
Print ("Length of string1 is", string. len (string1 ))
-- Repeating strings
RepeatedString = string. rep (string1, 3)
Print (repeatedString)
When we run the above program, we will get the following output.
The Code is as follows:
Concatenated string LuaTutorial
Length of string1 is 3
LuaLuaLua