【问题标题】:Lua - get indexOf stringLua - 获取 indexOf 字符串
【发布时间】:2013-11-26 16:43:43
【问题描述】:

我在 Lua 中遇到了一个问题,即检查字符串值是否没有出现在另一个字符串中。

这就是我在 Javascript 中可能会这样做的方式:

'my string'.indexOf('no-cache') === -1 // true

但在 Lua 中,我尝试使用 string 模块,这给了我意想不到的响应:

string.find('my string', 'no-cache') -- nil, that's fine but..
string.find('no-cache', 'no-cache') -- nil.. that's weird
string.find('no-cache', 'no') -- 1, 2 here it's right.. strange..

【问题讨论】:

    标签: string lua find indexof


    【解决方案1】:

    如前所述,- 是一个模式元字符,specifically

    • 后跟“-”的单个字符类,它也匹配该类中的 0 个或多个重复字符。与 '*' 不同,这些重复项将始终匹配最短的可能序列;

    您可能对string.findplain 选项感兴趣。这将避免将来逃避其他任何事情的需要。

    string.find('no-cache', 'no-cache', 1, true)
    

    【讨论】:

      【解决方案2】:

      - 是 lua 中的模式元字符。你需要逃避它。 string.find('no-cache', 'no%-cache')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-05
        • 1970-01-01
        • 2021-01-09
        • 2016-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多