我在Lua中有一个字符串,想要迭代其中的各个字符。但是我尝试过的代码都没有用,而官方手册仅显示了如何查找和替换子字符串:(
str = "abcd"
for char in str do -- error
print( char )
end
for i = 1, str:len() do
print( str[ i ] ) -- nil
end
Answers:
在lua 5.1中,您可以通过两种方式迭代字符串的字符。
基本循环为:
因为我= 1,#str做 局部c = str:sub(i,i) -用c做某事 结束
但是,使用模式string.gmatch()
来获得字符上的迭代器可能会更有效:
对于c in str:gmatch”。做 -用c做某事 结束
甚至string.gsub()
用于为每个字符调用一个函数:
str:gsub(“。”,function(c) -用c做某事 结束)
在以上所有内容中,我都利用了以下事实:将string
模块设置为所有字符串值的元表,因此可以使用:
符号将其功能称为成员。我还使用了(5.1的新版本,IIRC)#
来获取字符串长度。
为您的应用程序提供最佳答案取决于许多因素,如果性能至关重要,那么基准就是您的朋友。
您可能想要评估为什么需要遍历字符,并查看已绑定到Lua的正则表达式模块之一,或者以现代方法查看Roberto的lpeg模块,该模块为Lua实现了语法分析语法。
如果您使用的是Lua 5,请尝试:
for i = 1, string.len(str) do
print( string.sub(str, i, i) )
end
根据手头的任务,它可能更容易使用string.byte
。这也是最快的方法,因为它避免了创建新的子字符串,这在Lua中由于每个新字符串的散列并检查它是否已知而显得非常昂贵。您可以预先计算所需符号的代码,string.byte
以保持可读性和可移植性。
local str = "ab/cd/ef"
local target = string.byte("/")
for idx = 1, #str do
if str:byte(idx) == target then
print("Target found at:", idx)
end
end
提供的答案(此处,此处和此处)中已经有很多好的方法。如果您主要追求速度,那么您绝对应该考虑通过Lua的C API来完成这项工作,它比原始Lua代码快许多倍。当使用预加载的块(例如load function)时,差异不是很大,但是仍然相当可观。
至于纯粹的Lua解决方案,让我分享一下我已经制定的这个小型基准。它涵盖了迄今为止提供的所有答案,并增加了一些优化。尽管如此,要考虑的基本问题是:
您需要遍历字符串中的字符多少次?
这是完整的代码:
-- Setup locals
local str = "Hello World!"
local attempts = 5000000
local reuses = 10 -- For the second part of benchmark: Table values are reused 10 times. Change this according to your needs.
local x, c, elapsed, tbl
-- "Localize" funcs to minimize lookup overhead
local stringbyte, stringchar, stringsub, stringgsub, stringgmatch = string.byte, string.char, string.sub, string.gsub, string.gmatch
print("-----------------------")
print("Raw speed:")
print("-----------------------")
-- Version 1 - string.sub in loop
x = os.clock()
for j = 1, attempts do
for i = 1, #str do
c = stringsub(str, i)
end
end
elapsed = os.clock() - x
print(string.format("V1: elapsed time: %.3f", elapsed))
-- Version 2 - string.gmatch loop
x = os.clock()
for j = 1, attempts do
for c in stringgmatch(str, ".") do end
end
elapsed = os.clock() - x
print(string.format("V2: elapsed time: %.3f", elapsed))
-- Version 3 - string.gsub callback
x = os.clock()
for j = 1, attempts do
stringgsub(str, ".", function(c) end)
end
elapsed = os.clock() - x
print(string.format("V3: elapsed time: %.3f", elapsed))
-- For version 4
local str2table = function(str)
local ret = {}
for i = 1, #str do
ret[i] = stringsub(str, i) -- Note: This is a lot faster than using table.insert
end
return ret
end
-- Version 4 - function str2table
x = os.clock()
for j = 1, attempts do
tbl = str2table(str)
for i = 1, #tbl do -- Note: This type of loop is a lot faster than "pairs" loop.
c = tbl[i]
end
end
elapsed = os.clock() - x
print(string.format("V4: elapsed time: %.3f", elapsed))
-- Version 5 - string.byte
x = os.clock()
for j = 1, attempts do
tbl = {stringbyte(str, 1, #str)} -- Note: This is about 15% faster than calling string.byte for every character.
for i = 1, #tbl do
c = tbl[i] -- Note: produces char codes instead of chars.
end
end
elapsed = os.clock() - x
print(string.format("V5: elapsed time: %.3f", elapsed))
-- Version 5b - string.byte + conversion back to chars
x = os.clock()
for j = 1, attempts do
tbl = {stringbyte(str, 1, #str)} -- Note: This is about 15% faster than calling string.byte for every character.
for i = 1, #tbl do
c = stringchar(tbl[i])
end
end
elapsed = os.clock() - x
print(string.format("V5b: elapsed time: %.3f", elapsed))
print("-----------------------")
print("Creating cache table ("..reuses.." reuses):")
print("-----------------------")
-- Version 1 - string.sub in loop
x = os.clock()
for k = 1, attempts do
tbl = {}
for i = 1, #str do
tbl[i] = stringsub(str, i) -- Note: This is a lot faster than using table.insert
end
for j = 1, reuses do
for i = 1, #tbl do
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V1: elapsed time: %.3f", elapsed))
-- Version 2 - string.gmatch loop
x = os.clock()
for k = 1, attempts do
tbl = {}
local tblc = 1 -- Note: This is faster than table.insert
for c in stringgmatch(str, ".") do
tbl[tblc] = c
tblc = tblc + 1
end
for j = 1, reuses do
for i = 1, #tbl do
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V2: elapsed time: %.3f", elapsed))
-- Version 3 - string.gsub callback
x = os.clock()
for k = 1, attempts do
tbl = {}
local tblc = 1 -- Note: This is faster than table.insert
stringgsub(str, ".", function(c)
tbl[tblc] = c
tblc = tblc + 1
end)
for j = 1, reuses do
for i = 1, #tbl do
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V3: elapsed time: %.3f", elapsed))
-- Version 4 - str2table func before loop
x = os.clock()
for k = 1, attempts do
tbl = str2table(str)
for j = 1, reuses do
for i = 1, #tbl do -- Note: This type of loop is a lot faster than "pairs" loop.
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V4: elapsed time: %.3f", elapsed))
-- Version 5 - string.byte to create table
x = os.clock()
for k = 1, attempts do
tbl = {stringbyte(str,1,#str)}
for j = 1, reuses do
for i = 1, #tbl do
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V5: elapsed time: %.3f", elapsed))
-- Version 5b - string.byte to create table + string.char loop to convert bytes to chars
x = os.clock()
for k = 1, attempts do
tbl = {stringbyte(str, 1, #str)}
for i = 1, #tbl do
tbl[i] = stringchar(tbl[i])
end
for j = 1, reuses do
for i = 1, #tbl do
c = tbl[i]
end
end
end
elapsed = os.clock() - x
print(string.format("V5b: elapsed time: %.3f", elapsed))
输出示例(Lua 5.3.4,Windows):
-----------------------
Raw speed:
-----------------------
V1: elapsed time: 3.713
V2: elapsed time: 5.089
V3: elapsed time: 5.222
V4: elapsed time: 4.066
V5: elapsed time: 2.627
V5b: elapsed time: 3.627
-----------------------
Creating cache table (10 reuses):
-----------------------
V1: elapsed time: 20.381
V2: elapsed time: 23.913
V3: elapsed time: 25.221
V4: elapsed time: 20.551
V5: elapsed time: 13.473
V5b: elapsed time: 18.046
结果:
就我而言,string.byte
和string.sub
的原始速度最快。使用缓存表并在每个循环中重复使用10次时,string.byte
即使将字符码转换回chars ,该版本也是最快的(这并不总是必需的,取决于使用情况)。
您可能已经注意到,我根据以前的基准进行了一些假设,并将其应用于代码:
tbl[idx] = value
比table.insert(tbl, value)
。for i = 1, #tbl
速度比快一点for k, v in pairs(tbl)
。希望能帮助到你。
所有人都提出了一种不太理想的方法
最好:
function chars(str)
strc = {}
for i = 1, #str do
table.insert(strc, string.sub(str, i, i))
end
return strc
end
str = "Hello world!"
char = chars(str)
print("Char 2: "..char[2]) -- prints the char 'e'
print("-------------------\n")
for i = 1, #str do -- testing printing all the chars
if (char[i] == " ") then
print("Char "..i..": [[space]]")
else
print("Char "..i..": "..char[i])
end
end
迭代构造一个字符串,并使用load()返回此字符串作为表...
itab=function(char)
local result
for i=1,#char do
if i==1 then
result=string.format('%s','{')
end
result=result..string.format('\'%s\'',char:sub(i,i))
if i~=#char then
result=result..string.format('%s',',')
end
if i==#char then
result=result..string.format('%s','}')
end
end
return load('return '..result)()
end
dump=function(dump)
for key,value in pairs(dump) do
io.write(string.format("%s=%s=%s\n",key,type(value),value))
end
end
res=itab('KOYAANISQATSI')
dump(res)
扑灭...
1=string=K
2=string=O
3=string=Y
4=string=A
5=string=A
6=string=N
7=string=I
8=string=S
9=string=Q
10=string=A
11=string=T
12=string=S
13=string=I