Lua string


A string or string (String) is a string of characters composed of numbers, letters, and underscores.

Strings in Lua language can be represented in the following three ways:

  • A string of characters between single quotes.

  • A string of characters between double quotes. A string of characters between

  • [[and]].

String examples of the above three methods are as follows:

string1 = "Lua"
print("\"字符串 1 是\"",string1)
string2 = 'w3cschool.cc'
print("字符串 2 是",string2)

string3 = [["Lua 教程"]]
print("字符串 3 是",string3)

The output result of executing the above code is:

"字符串 1 是"	Lua
字符串 2 是	w3cschool.cc
字符串 3 是	"Lua 教程"

Escape characters are used to indicate that it cannot Directly displayed characters, such as the back key, enter key, etc. For example, you can use "\"" to convert double quotes in a string.

All escape characters and their corresponding meanings:

## MeaningASCII code value (decimal)\a \b\f\n##\r\t##011 represents a backslash character''\' represents a single quote (apostrophe) character ## represents a double quote character \0
## Conversion Definition of character
##BELL
007
Backspace (BS), moves the current position to the previous column
008
Page change (FF), moves the current position to the beginning of the next page
012
Line feed (LF), moves the current position to the beginning of the next line
010
Press Enter (CR) to move the current position to this line Beginning
013
Horizontal tab ( HT) (jump to the next TAB position)
009
##\v
Vertical Tabulation (VT)
##\\
092
\'
039
\"
034
##NULL character(NULL)
000
\ddd
Any character represented by 1 to 3 octal numbers
Three octal numbers
\xhh
Any character represented by 1 to 2 hexadecimal digits
two digits hexadecimal

String operations

Lua provides many methods to support string operations:

78910
Serial number Methods & Usage
1string.upper(argument):
Convert all strings to uppercase letters.
2string.lower(argument):
Convert all strings to lowercase letters.
3string.gsub(mainString,findString,replaceString,num)
Replace in the string, mainString is to be replaced string, findString is the character to be replaced, replaceString is the character to be replaced, num is the number of replacements (can be ignored, then all will be replaced), such as:
> string.gsub("aaaa","a","z",3);
zzza	3
4string.strfind (str, substr, [init, [end]])
Search for the specified content in a specified target string (the third parameter is the index) and return its specific Location. Returns nil if it does not exist.
> string.find("Hello Lua user", "Lua", 1) 
7	9
5string.reverse(arg)
String reverse
> string.reverse("Lua")
auL
6##string.format(...)Returns a printf-like formatted string
> string.format("the value is:%d",4)
the value is:4

string.char(arg) and string.byte(arg[,int])char converts integer numbers into characters and connects them, byte converts characters into integer values ​​(can Specify a certain character, defaulting to the first character).
> string.char(97,98,99,100)
abcd
> string.byte("ABCD",4)
68
> string.byte("ABCD")
65
>

string.len(arg)Calculate the string length.
string.len("abc")
3

string.rep(string, n))Return n copies of string string
> string.rep("abcd",2)
abcdabcd

..Concatenate two strings
> print("www.w3cschool".."cc")
www.w3cschoolcc

String Case conversion

The following example demonstrates how to convert string case:

string1 = "Lua";
print(string.upper(string1))
print(string.lower(string1))

The execution result of the above code is:

LUA
lua

String search and reversal

The following example demonstrates how to search and reverse a string:

string = "Lua Tutorial"
-- 查找字符串
print(string.find(string,"Tutorial"))
reversedString = string.reverse(string)
print("新字符串为",reversedString)

The execution result of the above code is:

5	12
新字符串为	lairotuT auL

String formatting

The following The example demonstrates how to format a string:

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 execution result of the above code is:

基本格式化 Lua Tutorial
日期格式化 02/01/2014
0.3333

Conversion of characters and integers

The following example demonstrates the conversion of characters and integers Mutual conversion:

-- 字符转换
-- 转换第一个字符
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 execution result of the above code is:

76
97
97
117
117
a

Other commonly used functions

The following examples demonstrate other string operations, such as calculating string length, string concatenation , string copy, etc.:

string1 = "www."
string2 = "w3cschool"
string3 = ".cc"
-- 使用 .. 进行字符串连接
print("连接字符串",string1..string2..string3)

-- 字符串长度
print("字符串长度 ",string.len(string2))

-- 字符串复制 2 次
repeatedString = string.rep(string2,2)
print(repeatedString)

The execution result of the above code is:

连接字符串	www.w3cschool.cc
字符串长度 	9
w3cschoolw3cschool