How to string.find the square bracket character in lua?

前端 未结 2 1327
南笙
南笙 2021-02-14 11:21

So I\'m trying to find square brackets inside a string:

s = \"testing [something] something else\"
x,y = string.find(s,\"[\")

which gives me an

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 12:12

    John's answer will work -- turning off pattern matching.

    What you're trying to do -- escape the [ -- is done with the % character in Lua:

     x,y = string.find(s,'%[')
    

    Also strings in Lua all have the string module as their metatable, so you could just say:

     x,y = s:find('%[')
    

    or even:

     x,y = s:find'%['
    

提交回复
热议问题