Lua - 获取indexOf字符串 [英] Lua - get indexOf string

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

问题描述

我在Lua中遇到了一个问题,检查字符串值是否不在另一个字符串中。

I am stuck with a problem in Lua to check whether the string value is not presented in another string.

这就是我可能会用Javascript来做的: / p>

That's how I likely will do it in Javascript:

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

但是在Lua中我试图使用 string 模块,它给了我意外的回应:

but in Lua I'm trying to use string module which gives me unexpected response:

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


推荐答案

已被提及, - 是模式元字符,特别是

As has already been mentioned, - is a pattern metacharacter, specifically:



  • 后跟' - '的单个字符类,也匹配0或更多的重复的字符班里的同学与*不同,这些重复项目将始终匹配最短的序列;

您可能对 plain 选项 string.find

You might be interested in the plain option for string.find. This will avoid the need for escaping anything else in the future.

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

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

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