string - Lua - 获取 indexOf 字符串

标签 string lua find indexof

我在 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..

最佳答案

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

  • a single character class followed by '-', which also matches 0 or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;


您可能对 plain 感兴趣 string.find 的选项.这将避免将来逃避其他任何事情的需要。
string.find('no-cache', 'no-cache', 1, true)

关于string - Lua - 获取 indexOf 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20222916/

相关文章:

c - 在 C 中简单搜索文件

php - 如何用另一个字符串替换字符串并在 php 和 mysql 中保持大小写?

c++ - 有人可以解释为什么我想要或需要将 Lua 与 C++ 混合用于游戏吗?

bash - 查找具有不同 umask 的文件和目录

unix - 仅列出目录中匹配单词 BOZO 并以 '123' 或 '456' 结尾的某些文件

用于打印 bash 颜色的 python 类

lua - 我可以将 `init.lua` 与 Hammerspoon 中的不同组件分开吗?

lua - 尝试索引全局 'io'(零值)

Python/PyGame : Find pixel by colorcode

javascript - 如何获取字符串中的最后一个字符?