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:
## Conversion Definition of character | ## MeaningASCII code value (decimal) | ||||||||||||||||||||||
##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 | ##\r | |||||||||||||||||||||
Press Enter (CR) to move the current position to this line Beginning | 013 | \t | |||||||||||||||||||||
Horizontal tab ( HT) (jump to the next TAB position) | 009 | ##\v | |||||||||||||||||||||
Vertical Tabulation (VT) | ##011##\\ | ||||||||||||||||||||||
092 | \' | ||||||||||||||||||||||
039 | \" | ## represents a double quote character||||||||||||||||||||||
034 | \0 | ##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 operationsLua provides many methods to support string operations:
string1 = "Lua"; print(string.upper(string1)) print(string.lower(string1))The execution result of the above code is: LUA luaString search and reversalThe 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 auLString formattingThe 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.3333Conversion of characters and integersThe 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 aOther commonly used functionsThe 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 |