Lua - 获得所有的词后

问题描述 投票:0回答:1

有这样的if语句。if( reciewed_message == "video " ) then

我需要得到所有的文本后 'video' 变成一个字符串。

例如,对于字符串 reciewed_message == "video some music video" 我需要得到 some music videoreciewed_message 作为搜索的参数

string search lua
1个回答
2
投票

使用 string.match 与捕捉。

s="video some music video"
print(s:match("video (.*)$"))

0
投票

你可以使用 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"))

© www.soinside.com 2019 - 2024. All rights reserved.