【问题标题】:Lua Split string up and put output into a tableLua拆分字符串并将输出放入表中
【发布时间】:2020-05-23 23:21:32
【问题描述】:

我正在尝试使用 Lua 以尽可能好的方式拆分字符串

我想要达到的输出是这样的。

"is"
"this"
"ents"
"cont"
"_"
etc
etc

这是我到目前为止没有成功的代码

local variable1 = "this_is_the_string_contents"

local l = string.len(variable1)
local i = 0
local r = nil
local chunks = {} --table to store output in

while i < l do
r = math.random(1, l - i)

--if l - i > i then
--r = math.random(1, (l - i) / 2)
--else
--r = math.random(1, (l - i))
--end

print(string.sub(variable1, i, r))

chunks = string.sub(variable1, i, r)
i = i+r 

end

【问题讨论】:

  • to split a string up in the best way possible - “以最好的方式”是什么意思?
  • 为什么contents被拆分成contents?您是否正在寻找最多 4 个字母的长度,除非您点击 _
  • @Nifim 我的目标是平衡它老实说它不必是 4 只是为了将整个字符串分成相等的部分是我的目标。
  • chunks = string.sub(variable1, i, r) 将变量chunks 设置为该变量,并将其转换为字符串而不是表格。你会想要table.insert(chunks, string.sub(variable1, i, r)。但否则你会得到什么输出?

标签: lua


【解决方案1】:

如果我理解正确,您想将字符串分成相等的部分。以下将准确执行此操作并将其存储到表中。

local variable2 = "this_is_the_string_contents"

math.randomseed(os.time())
local l = #variable2
local i = 0
local r = math.random(1, l/2)
local chunks = {}

while i <= l+5 do
    print(variable2:sub(i, i+r))
    table.insert(chunks, variable2:sub(i,i+r))

    i = i+r+1
end

每次运行脚本时更改math.randomseed 是一个好习惯,这样随机数会有更多变化。不过,快速细分。

local r = math.random(1, l/2):您可以将 2 更改为您想要的任何值,但这会阻止脚本将 #variable2 分配为长度,从而可以将变量作为单个块获取。

while i &lt;= l+5 do:我添加了+5 来说明一些超额情况,只是作为预防措施。

table.insert(chunks, variable2:sub(i, i+r)): 这就是你需要插入到表中的内容。由于我们想要相等的数量,因此您将使用 i+r 作为结束子。

i = i+r+1: 你不想重复字母。

最终结果如下所示:

Pass One:
this_is_the
_string_cont
ents   

Pass Two:
thi
s_is
_the
_str
ing_
cont
ents

等等。如果这不是您想要的,请建议并修改您的问题。如果你想单独存储_而不是单词的一部分,那会更容易,但是你描述它的方式,你说得均匀,所以我暂时保留它。

【讨论】:

    【解决方案2】:

    您似乎想为每个字符串创建一个随机长度?

    function GetRandomStringList(str)
        local str_table = {}
        while string.len(str)>0 do
            local str_chunk_size = math.random(1,string.len(str))
            table.insert(str_table,string.sub(str,1,str_chunk_size))
            str=string.sub(str,str_chunk_size+1)
        end
        return str_table
    end
    
    function DisplayStringList(name, str_table)
        print(name..":")
        for loop=1,#str_table do
            print(str_table[loop])
        end
        print("")
    end
    
    do
        local str = "this_is_the_string_contents"
        DisplayStringList("first", GetRandomStringList(str))
        DisplayStringList("second", GetRandomStringList(str))
        DisplayStringList("third", GetRandomStringList(str))
    end
    

    我只是在字符串中仍然存在字符时循环,随机选择一个块大小,将字符串的该部分插入到表中,然后从字符串中删除该部分。重复。当字符串为空时,返回表给调用者处理。

    输出如下:

    first:
    t
    his_is_the_stri
    ng_
    content
    s
    
    second:
    this_is_the_s
    tring
    _contents
    
    third:
    this_is_the_string_cont
    ent
    s
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 2015-01-16
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多