Lua中修改字符串中的字符 [英] Modifying a character in a string in Lua

查看:46
本文介绍了Lua中修改字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Lua中替换字符串中位置N处的字符.

Is there any way to replace a character at position N in a string in Lua.

这是我目前想到的:

function replace_char(pos, str, r)
    return str:sub(pos, pos - 1) .. r .. str:sub(pos + 1, str:len())
end

str = replace_char(2, "aaaaaa", "X")
print(str)

我也不能使用 gsub,因为它会替换每个捕获,而不仅仅是位置 N 的捕获.

I can't use gsub either as that would replace every capture, not just the capture at position N.

推荐答案

Lua 中的字符串是不可变的.这意味着,任何替换字符串中文本的解决方案最终都必须构造一个具有所需内容的新字符串.对于将单个字符替换为其他内容的特定情况,您需要将原始字符串拆分为前缀部分和后缀部分,并将它们重新连接到新内容周围.

Strings in Lua are immutable. That means, that any solution that replaces text in a string must end up constructing a new string with the desired content. For the specific case of replacing a single character with some other content, you will need to split the original string into a prefix part and a postfix part, and concatenate them back together around the new content.

代码的这种变化:

function replace_char(pos, str, r)
    return str:sub(1, pos-1) .. r .. str:sub(pos+1)
end

是对简单 Lua 的最直接的翻译.对于大多数用途来说,它可能足够快.我已经修复了前缀应该是第一个 pos-1 字符的错误,并利用了如果 string.sub 的最后一个参数丢失它的事实假定为 -1,相当于字符串的结尾.

is the most direct translation to straightforward Lua. It is probably fast enough for most purposes. I've fixed the bug that the prefix should be the first pos-1 chars, and taken advantage of the fact that if the last argument to string.sub is missing it is assumed to be -1 which is equivalent to the end of the string.

但请注意,它会创建一些临时字符串,这些字符串将在字符串存储中徘徊,直到垃圾收集将它们吃掉.任何解决方案都无法避免前缀和后缀的临时性.但这也必须为第一个 .. 运算符创建一个临时对象,以供第二个使用.

But do note that it creates a number of temporary strings that will hang around in the string store until garbage collection eats them. The temporaries for the prefix and postfix can't be avoided in any solution. But this also has to create a temporary for the first .. operator to be consumed by the second.

两种替代方法之一可能会更快.第一个是 Paŭlo Ebermann 提供的解决方案,但有一个小调整:

It is possible that one of two alternate approaches could be faster. The first is the solution offered by Paŭlo Ebermann, but with one small tweak:

function replace_char2(pos, str, r)
    return ("%s%s%s"):format(str:sub(1,pos-1), r, str:sub(pos+1))
end

这使用 string.format 来组装结果,希望它可以猜测最终的缓冲区大小,而无需额外的临时对象.

This uses string.format to do the assembly of the result in the hopes that it can guess the final buffer size without needing extra temporary objects.

但请注意,string.format 可能会遇到任何通过其 %s 字符的问题> 格式.具体来说,由于它是根据标准 C 的 sprintf() 函数实现的,因此期望它在第一次出现 时终止替换的字符串是合理的.(用户 Delusional Logic 在评论中指出.)

But do beware that string.format is likely to have issues with any characters in any string that it passes through its %s format. Specifically, since it is implemented in terms of standard C's sprintf() function, it would be reasonable to expect it to terminate the substituted string at the first occurrence of . (Noted by user Delusional Logic in a comment.)

想到的第三种选择是:

function replace_char3(pos, str, r)
    return table.concat{str:sub(1,pos-1), r, str:sub(pos+1)}
end

table.concat 有效地将字符串列表连接成最终结果.它有一个可选的第二个参数,它是要在字符串之间插入的文本,默认为 "" 这符合我们的目的.

table.concat efficiently concatenates a list of strings into a final result. It has an optional second argument which is text to insert between the strings, which defaults to "" which suits our purpose here.

我的猜测是,除非您的字符串很大并且您经常进行此替换,否则您将看不到这些方法之间的任何实际性能差异.但是,我之前也感到很惊讶,因此请分析您的应用程序以验证是否存在瓶颈,并仔细对潜在解决方案进行基准测试.

My guess is that unless your strings are huge and you do this substitution frequently, you won't see any practical performance differences between these methods. However, I've been surprised before, so profile your application to verify there is a bottleneck, and benchmark potential solutions carefully.

这篇关于Lua中修改字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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