如何在Lua字符串中迭代单个字符?

时间:2009-05-06 10:55:37

标签: lua

我在Lua中有一个字符串,想要在其中迭代单个字符。但是我没有试过的代码和官方手册只显示如何查找和替换子串:(

str = "abcd"
for char in str do -- error
  print( char )
end

for i = 1, str:len() do
  print( str[ i ] ) -- nil
end

6 个答案:

答案 0 :(得分:113)

在lua 5.1中,你可以通过几种方式迭代字符串的字符。

基本循环是:

for i = 1, #str do
    local c = str:sub(i,i)
    -- do something with c
end

但是使用string.gmatch()模式来获取字符上的迭代器可能更有效:

for c in str:gmatch"." do
    -- do something with c
end

甚至使用string.gsub()为每个字符调用函数:

str:gsub(".", function(c)
    -- do something with c
end)

在上述所有内容中,我利用了string模块被设置为所有字符串值的元表的事实,因此可以使用:将其函数称为成员符号。我还使用了(新的5.1,IIRC)#来获取字符串长度。

您的应用程序的最佳答案取决于很多因素,如果性能变得重要,基准测试就是您的朋友。

您可能想要评估为什么需要迭代字符,并查看已绑定到Lua的正则表达式模块之一,或者查看现代方法查看Roberto的{ {3}}模块,用于为Lua实现解析表达式语法。

答案 1 :(得分:11)

如果您使用的是Lua 5,请尝试:

for i = 1, string.len(str) do
    print( string.sub(str, i, i) )
end

答案 2 :(得分:5)

根据手头的任务,使用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

答案 3 :(得分:3)

提供的答案中已经有很多好方法(hereherehere)。如果速度是您主要所寻找的,那么您应该考虑通过Lua的C API来完成这项工作,这比原始Lua代码快许多倍。使用预加载的块(例如load function)时,差异不大,但仍然相当大。

至于 Lua解决方案,让我分享这个小基准,我已经做过。它涵盖了此日期提供的每个答案,并添加了一些优化。不过,最基本的考虑因素是:

你需要多少次迭代字符串中的字符?

  • 如果答案是"一次",那么你应该查看banchmark的第一部分("原始速度")。
  • 否则,第二部分将提供更精确的估计,因为它将字符串解析为表,迭代的速度要快得多。您还应该考虑为此编写一个简单的函数,例如@Jarriz建议。

以下是完整代码:

-- 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.bytestring.sub在原始速度方面最快。使用缓存表并在每个循环中重复使用10次时,string.byte版本即使将字符代码转换回字符时也是最快的(这不是必需的,并且取决于使用情况)。

正如您可能已经注意到的那样,我已根据之前的基准做出了一些假设并将其应用于代码:

  1. 如果在循环内使用库函数应始终本地化,因为它要快得多。
  2. 使用tbl[idx] = value而不是table.insert(tbl, value),将新元素插入lua表要快得多。
  3. 使用for i = 1, #tbl循环播放表比for k, v in pairs(tbl)快一点。
  4. 总是更喜欢具有较少函数调用的版本,因为调用本身会增加执行时间。
  5. 希望它有所帮助。

答案 4 :(得分:0)

所有人都建议采用不太理想的方法

将是最好的:

    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

答案 5 :(得分:0)

迭代构造一个字符串,并使用load()返回此字符串作为表...

const staffTotalPrices = (eventFirebaseKey) => new Promise((resolve, reject) => {
  eventStaff.getEventStaff(eventFirebaseKey).then((staffArray) => {
    let staffTotal = 0;
    staffArray.forEach((staff) => {
      staffData.getSingleStaff(staff.staffUid).then((staffObject) => {
        staffTotal += parseInt(staffObject.price, 10);
        return staffTotal;
      });
    });
    resolve(staffTotal);
  }).catch((error) => reject(error));
});

推出...

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)