【问题标题】:Why does lua string.find returns wrong number为什么lua string.find返回错误的数字
【发布时间】:2020-04-01 21:53:30
【问题描述】:
local num = string.find("i want this to work --", "--")
return num

所以这段代码应该返回 21 但它实际上返回 1

我做错了什么?

【问题讨论】:

    标签: lua


    【解决方案1】:

    - 是 Lua 模式中的特殊字符。要进行文字匹配,string.find 的第二个参数必须是 '%-%-'

    或者,如果您不想处理模式语义,您可以传递true 的第四个参数来告诉string.find 将第二个参数作为文字字符串而不是模式:

    string.find("i want this to work --", "--", 1, true)
    

    【讨论】:

    • 或者你可以使用 string.find 的最后一个参数来设置凋谢或不将模式作为纯文本:string.find("some--thing", "--", 1, true)
    • @Nifim:我没想到。我将其添加到我的答案中。
    猜你喜欢
    • 2017-08-08
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多