在 Lua 示例中查找两个字符串文本的"差异"
2017-1-6 19:51:0
收藏:0
阅读:93
评论:2
我正在尝试在 Lua 中找到两个字符串值之间文本的差异,但是我不太确定如何有效地做到这一点。我在使用字符串模式方面缺乏经验,我相信这是我失败的原因。这是一个示例:
-- 原始文本
local text1 = "hello there"
-- 改变的文本
local text2 = "hello.there"
-- 使用某个"模式"找到原始文本的改变
print(text2:match("pattern"))
在上面的示例中,我想输出文本"。",因为那是两个文本之间的差异。对于差异可能敏感于字符串模式的案例,也是如此,例如:
local text1 = "hello there"
local text2 = "hello()there"
print(text2:match("pattern"))
在这个例子中,我想要打印"(",因为在那时新的字符串不再与旧的字符串一致了。
如果有人对此有任何见解,我会非常感激。很抱歉我不能给出更多的代码工作,我不确定从哪里开始。
点赞
用户3979429
只需迭代字符串并找出它们不匹配的位置。
function StringDifference(str1,str2)
for i = 1,#str1 do --循环字符串
if str1:sub(i,i) ~= str2:sub(i,i) then --如果该字符不等于它的对应字符
return i --返回该索引
end
end
return #str1+1 --返回较短的字符串结束后的索引作为备用。
end
print(StringDifference("hello there", "hello.there"))
2017-01-06 23:18:29
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

local function get_inserted_text(old, new) local prv = {} for o = 0, #old do prv[o] = "" end for n = 1, #new do local nxt = {[0] = new:sub(1, n)} local nn = new:sub(n, n) for o = 1, #old do local result if nn == old:sub(o, o) then result = prv[o-1] else result = prv[o]..nn if #nxt[o-1] <= #result then result = nxt[o-1] end end nxt[o] = result end prv = nxt end return prv[#old] end用法:
print(get_inserted_text("hello there", "hello.there")) --> . print(get_inserted_text("hello there", "hello()there")) --> () print(get_inserted_text("hello there", "hello htere")) --> h print(get_inserted_text("hello there", "heLlloU theAre")) --> LUA