Lua string

String or strings (String) is a string of characters consists of numbers, letters, underscores thereof.

Lua language strings can be expressed using the following methods:

  • Single string of characters between the quotation marks.
  • A string of characters between the double quotes.
  • [[And]] between a string of characters.

Examples of the above string of three ways as follows:

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

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

The output is the above code is executed:

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

Escape character represents can not be displayed directly, such as the back key, the Enter key, and so on. As in double quotation marks can be used to convert the string "\". "

All the escape character and the corresponding meanings:

Escape character
significance
ASCII value (decimal)
\ A
Bell (BEL)
007
\ B
Backspace (BS), the current position to the previous one
008
\ F
Form feed (FF), the current position to the beginning of the next page
012
\ N
Line feed (LF), the current position to the beginning of the next line
010
\ R
A carriage return (CR), the current position to the beginning of the line
013
\ T
Horizontal tab (HT) (skip to the next TAB position)
009
\ V
Vertical Tab (VT)
011
\\
Represent a backslash character '\'
092
\ '
On behalf of a single quote (apostrophe) character
039
\ "
It represents a double-quote character
034
\ 0
Null character (NULL)
000
\ Ddd
Any character 1-3 octal number represented
Three octal
\ Xhh
1-2 hexadecimal character represents any
Two hexadecimal

String Manipulation

Lua provides many ways to support the operation of the string:

No. Method & Purpose
1 string.upper (argument):
String all uppercase letters.
2 string.lower (argument):
String all lowercase letters.
3 string.gsub (mainString, findString, replaceString, num)
In the replacement string, mainString want to replace the string, findString for the characters that were replaced, replaceString to replace characters, num Replace Occurrence (can be ignored, then replace all), such as:
> string.gsub("aaaa","a","z",3);
zzza	3
4 string.strfind (str, substr, [init , [end]])
Search for specified content (the third parameter for the index) at a specified target string and returns its location. Does not exist, it returns nil.
> string.find("Hello Lua user", "Lua", 1) 
7	9
5 string.reverse (arg)
To reverse a string
> string.reverse("Lua")
auL
6 string.format (/en.)
Returns a printf-like format strings
> string.format("the value is:%d",4)
the value is:4
7 string.char (arg) and string.byte (arg [, int])
char integer numbers will turn into characters and connections, byte characters converted to an integer value (you can specify a character, the first character by default).
> string.char(97,98,99,100)
abcd
> string.byte("ABCD",4)
68
> string.byte("ABCD")
65
>
8 string.len (arg)
Compute string length.
string.len("abc")
3
9 string.rep (string, n))
Returns n copies of the string string
> string.rep("abcd",2)
abcdabcd
10 /en
Link two strings
> print("www.w3cschool"/en"cc")
www.w3cschoolcc

String case conversion

The following example demonstrates how to convert a string case:

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

The above code is executed as a result of:

LUA
lua

String search and reverse

The following example shows how the string to find and reverse operation:

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

The above code is executed as a result of:

5	12
新字符串为	lairotuT auL

String formatting

The following 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 above code is executed as a result of:

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

Character and integer conversion

The following examples demonstrate the character and integer 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 above code is executed as a result of:

76
97
97
117
117
a

Other commonly used functions

The following examples demonstrate other string operations, such as calculating the string length, string concatenation, string replication:

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

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

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

The above code is executed as a result of:

连接字符串	www.w3resource.net
字符串长度 	9
w3cschoolw3cschool