如何使用lua打乱单词的字母 [英] How to shuffle the letters of a word using lua

查看:47
本文介绍了如何使用lua打乱单词的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 中使用了这个 str_shuffle() 函数和 meka 这个 api我需要做同样的想法洗牌字母之间有空格但用lua与电报机器人一起工作,我搜索了很多,但没有找到类似的

I used this str_shuffle() function in PHP and meka this api I need to do the same idea shuffle the letters With space between them but with lua to working with telegram bots I searched a lot and found nothing similar

推荐答案

math.randomseed(os.time())

local function shuffle(str)
   local letters = {}
   for letter in str:gmatch'.[\128-\191]*' do
      table.insert(letters, {letter = letter, rnd = math.random()})
   end
   table.sort(letters, function(a, b) return a.rnd < b.rnd end)
   for i, v in ipairs(letters) do letters[i] = v.letter end
   return table.concat(letters)
end

print(shuffle("How to shuffle the whole UTF-8 string"))

这篇关于如何使用lua打乱单词的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆