有这样的if语句。if( reciewed_message == "video " ) then
我需要得到所有的文本后 'video'
变成一个字符串。
例如,对于字符串 reciewed_message == "video some music video"
我需要得到 some music video
从 reciewed_message
作为搜索的参数
使用 string.match
与捕捉。
s="video some music video"
print(s:match("video (.*)$"))
你可以使用 string.find
随后 string.sub
.
string.find
搜索字符串,并找到它被赋予的第一个查询的起始和结束位置。string.sub
从你要求的位置返回你的字符串的子字符串。
local received_message = "video some music video"
local query = "video"
-- First parameter is the starting position, we actually want the position that our word ends at, so we ignore the start position by '_'.
local _, len = string.find(received_message, query)
-- Sub-string is zero indexed, so we must add 1 to our length.
local search = string.sub(received_message, len + 1)
print(search)
你也可以创建一个函数,这样会更容易多次使用它。
function search_query(message, query)
local _, len = string.find(message, query)
return string.sub(message, len + 1)
end
-- Usage:
print(search_query("video some music video", "video"))