英文:
How would i split a string once every n characters
问题
让我们假设我有一个字符串:
local n = 2
local Name = "JohnIsNice"
如何将它每隔n个字符拆分,使字符串变成
{"Jo", "hn", "Is", "Ni", "Ce"}
我不太清楚如何做,所以任何帮助将不胜感激。
英文:
Let's say i have a string:
local n = 2
local Name = "JohnIsNice"
How would i split it every n characters so the string will become
{"Jo", "hn", "Is", "Ni", "Ce"}
I don't really know how to do it so any help would be appreciated
答案1
得分: 2
('.'):rep(n)
会生成一个包含 n 个点的字符串,表示匹配 n 个字符的模式。
Name:gmatch()
返回所有匹配项。
英文:
for m in Name:gmatch(('.'):rep(n)) do
print(m)
end
('.'):rep(n)
will generate a string with n dots which represents a pattern matches n characters.
Name:gmatch()
returns all the matches.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论