May 12, 2021 Lua
A string or string is a string of characters consisting of numbers, letters, and underscores.
Strings in the Lua language can be represented in three ways:
Examples of strings in these three ways are as follows:
string1 = "Lua" print("\"字符串 1 是\"",string1) string2 = 'w3cschool.cn' print("字符串 2 是",string2)
string3 = [["Lua 教程"]] print("字符串 3 是",string3)
The above code executes the output as follows:
"字符串 1 是" Lua 字符串 2 是 w3cschool.cn 字符串 3 是 "Lua 教程"
Escape characters are used to represent characters that cannot be displayed directly, such as back keys, enter keys, and so on. For example, you can use the """" in string conversion double quotes.
All escape characters and corresponding meanings:
Escape characters
|
Significance
|
ASCII code value (de-progress)
|
\a
|
Bell (BEL)
|
007
|
\b
|
Drop back (BS) and move the current position to the previous column
|
008
|
\f
|
Page change (FF), move the current position to the beginning of the next page
|
012
|
\n
|
Line-by-line (LF), moving the current position to the beginning of the next line
|
010
|
\r
|
Return (CR) to move the current position to the beginning of the Line
|
013
|
\t
|
Horizontal tabulation (HT) (jump to the next TAB position)
|
009
|
\v
|
Vertical watch making (VT)
|
011
|
\\
|
Represents a backslash character
|
092
|
\'
|
Represents a single quote (apostrophe) character
|
039
|
\"
|
Represents a double quote character
|
034
|
|
Empty Characters (NULL)
|
000
|
\ddd
|
Any character represented by a 1- to 3-bit octal number
|
Three-bit octals
|
\xhh
|
Any character represented by 1 to 2 hens
|
Two heteens
|
Lua provides many ways to support string operations:
Serial number | Methods and uses |
---|---|
1 |
string.upper(argument):
Strings are all converted to capital letters. |
2 |
string.lower(argument):
Strings are all converted to lowercase letters. |
3 |
string.gsub(mainString,findString,replaceString,num)
Replace in string, mainString for the string to be replaced, findString for the replaced character, replaceString to replace the character, num replacement number (negligible, then replace all), such as: > string.gsub("aaaa","a","z",3); zzza 3 |
4 |
string.find (str, substr, [init, [end]])
Search for the specified content in a specified target string (the third argument is the index) and return its specific location. I f it does not exist, nil is returned. > string.find("Hello Lua user", "Lua", 1) 7 9 |
5 |
string.reverse(arg)
String inverse > string.reverse("Lua") auL |
6 |
string.format(...)
Returns a formatted string similar to printf > string.format("the value is:%d",4) the value is:4 |
7 |
string.char (arg) and string.byte (arg, int)
char turns the integer number into a character and connects it, and byte converts the character to an integer value (you can specify a character, the default first character). > string.char(97,98,99,100) abcd |
8 |
string.len(arg)
The length of the string is calculated. string.len("abc") 3 |
9 |
string.rep(string, n))
Returns n copies of the string string string > string.rep("abcd",2) abcdabcd |
10 |
..
Link two strings > print("www.w3cschool"..".cn") www.w3cschool.cn |
The following example shows how to convert string case:
string1 = "Lua"; print(string.upper(string1)) print(string.lower(string1))
The above code execution results are:
LUA lua
The following example shows how to find and reverse strings:
string = "Lua Tutorial" -- 查找字符串 print(string.find(string,"Tutorial")) reversedString = string.reverse(string) print("新字符串为",reversedString)
The above code execution results are:
5 12 新字符串为 lairotuT auL
The following example shows how strings can be formatted:
string1 = "Lua" string2 = "Tutorial" number1 = 10 number2 = 20 -- 基本字符串格式化 print(string.format("基本格式化 %s %s",string1,string2)) -- 日期格式化 date = 2; month = 1; year = 2014 print(string.format("日期格式化 %02d/%02d/%03d", date, month, year)) -- 十进制格式化 print(string.format("%.4f",1/3))
The above code execution results are:
基本格式化 Lua Tutorial 日期格式化 02/01/2014 0.3333
The following example demonstrates the conversion of characters and integers to each other:
-- 字符转换 -- 转换第一个字符 print(string.byte("Lua")) -- 转换第三个字符 print(string.byte("Lua",3)) -- 转换末尾第一个字符 print(string.byte("Lua",-1)) -- 第二个字符 print(string.byte("Lua",2)) -- 转换末尾第二个字符 print(string.byte("Lua",-2))
-- 整数 ASCII 码转换为字符 print(string.char(97))
The above code execution results are:
76 97 97 117 117 a
The following example demonstrates other string operations, such as calculating string length, string connection, string copy, and so on:
string1 = "www." string2 = "w3cschool" string3 = ".cn" -- 使用 .. 进行字符串连接 print("连接字符串",string1..string2..string3)
-- 字符串长度 print("字符串长度 ",string.len(string2))
-- 字符串复制 2 次 repeatedString = string.rep(string2,2) print(repeatedString)
The above code execution results are:
连接字符串 www.w3cschool.cn 字符串长度 9 w3cschoolw3cschool