Strings
Lua 也支持字符串类型 (例如. 文本) . 创建字符串, 使用 "双引号"
或 '单引号' 引用文本即可
:
> print("hello")
hello
我们可以采用下面方法声明字符串变量:
> who = "Lua user"
> print(who)
Lua user
我们也可以使用
..
操作符, 将字符串整合在一起:
> print("hello ")
hello
> print("hello " .. who) -- the variable "who" was assigned above
hello Lua user
> print(who)
Lua user
与其它语言不同, 你不能使用 +
操作符连接字符串. 例如.:
> message = "hello " + who
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
stdin:1: in main chunk
[C]: ?
更多 Lua's string 类型信息, 请查阅 StringsTutorial.
Strings Tutorial | |
Quotes
参考手册 2.1 节, 介绍了字符串 [1]. 单引号, 双引号, 两个中括号, 可用于定义 字符串.
> = "hello"
hello
> = 'hello'
hello
> = [[hello]]
hello
为何会有这么多方法, 用于定义字符串? 这样做, 主要是为了让你关闭某种类型中的其他引用. 例如.,
> = 'hello "Lua user"'
hello "Lua user"
> = "Its [[content]] hasn't got a substring."
Its [[content]] hasn't got a substring.
> = [[Let's have more "strings" please.]]
Let's have more "strings" please.
双括号的字符串, 还有一些其他的属性, 下面会提到.
Escape sequences (转义序列)
Lua 可处理类似 C 的转义序列. 参考手册 2.1 节, 有详细的说明 [1].
> = "hello \"Lua user\""
hello "Lua user"
> = 'hello\nNew line\tTab'
hello
New line Tab
如果使用了双中括号, 转义序列将不会生效, 例如:
> = [[hello\nNew line\tTab]]
hello\nNew line\tTab
Multiline quotes (多行引用)
双中括号, 可用于闭合跨越多行的字符串. 例如:
> = [[Multiple lines of text
>> can be enclosed in double square
>> brackets.]]
Multiple lines of text
can be enclosed in double square
brackets.
Nesting quotes (嵌套引用)
双中括号结构允许嵌套, 但是它们需要一个或多个插入在最外层的括号来区分它们.插入多少 = , 无关紧要, 只要起始与结束的括号数目相同即可.
> = [[one [[two]] one]] -- bad
stdin:1: nesting of [[...]] is deprecated near '['
> = [=[one [[two]] one]=] -- ok
one [[two]] one
> = [===[one [[two]] one]===] -- ok too
one [[two]] one
> = [=[one [ [==[ one]=] -- ok. nothing special about the inner content.
one [ [==[ one
Concatenation (串接)
使用操作符 ..
, 可以将字符串连接在一起.例如,
> = "hello" .. " Lua user"
hello Lua user
> who = "Lua user"
> = "hello "..who
hello Lua user
数值同样可以连接字符串. 这种情况下, 数值会自动被转换为字符串, 然后进行拼接操作.
> = "Green bottles: "..10
Green bottles: 10
> = type("Green bottles: "..10)
string
大量的连接操作处理, 会很慢, 因为每次连接操作需要在内存中申请一个新的字符串空间.下面的三个例子会返回相同的结果, 不同的是, 第一个可能非常慢:
-- slow
local s = ''
for i=1,10000 do s = s .. math.random() .. ',' end
io.stdout:write(s)
-- fast
for i=1,10000 do io.stdout:write(tostring(math.random()), ',') end
-- fast, but uses more memory
local t = {}
for i=1,10000 do t[i] = tostring(math.random()) end
io.stdout:write(table.concat(t,','), ',')
The string library (字符串库)
Lua 的标准库里面, 提供了许多有用的函数, 可用于处理和操纵字符串.更多细节, 请查阅 StringLibraryTutorial.下面是字符串库的一些例子.
> = string.byte("ABCDE", 2) -- return the ASCII value of the second character
66
> = string.char(65,66,67,68,69) -- return a string constructed from ASCII values
ABCDE
> = string.find("hello Lua user", "Lua") -- find substring "Lua"
7 9
> = string.find("hello Lua user", "l+") -- find one or more occurrences of "l"
3 4
> = string.format("%.7f", math.pi) -- format a number
3.1415927
> = string.format("%8s", "Lua") -- format a string
Lua
Coercion (多态)
Lua 在恰当的时候, 会自动实现字符串与数值的互转. 这被称为 多态.
> = "This is Lua version " .. 5.1 .. " we are using."
This is Lua version 5.1 we are using.
> = "Pi = " .. math.pi
Pi = 3.1415926535898
> = "Pi = " .. 3.1415927
Pi = 3.1415927
如上所述, 多态过程中, 我们不用全程控制格式化过程. 将数值格式化为指定格式, 可使用
string.format()
函数. 例如.,
> = string.format("%.3f", 5.1)
5.100
> = "Lua version " .. string.format("%.1f", 5.1)
Lua version 5.1
这里使用的函数来完成显示转换, 而并非隐式的多态转变. 更多细节, 请查阅
NumbersTutorial.
String 库
关于String库的用法, 在此不做详细的介绍, 详细请查阅: http://lua-users.org/wiki/StringLibraryTutorial
string.char(i1, i2, ...)
> for k, v in pairs(strobj) do print('', k, v) end
char function: 0x41e6d0
find function: 0x41fd30
reverse function: 0x41e0f0
lower function: 0x41e340
sub function: 0x41ef40
rep function: 0x41e190
upper function: 0x41e040
match function: 0x41fd20
len function: 0x41e310
gmatch function: 0x41e680
gsub function: 0x41fd40
format function: 0x41e810
dump function: 0x41f3f0
byte function: 0x41f020
string.dump(function)
string.find(s, pattern, index, plain)
string.format(s, e1, e2)
string.upper(argument): ---- string:upper()
string.lower(argument): ---- string:lower()
string.gsub(mainString,findString,replaceString)
string.len(arg)
string.sub(s, i, j)
string.reverse(arg)