Other Languages Moderators Lord Henry Posted November 1, 2017 Other Languages Moderators Posted November 1, 2017 Uma micro-dúvida rápida galera, preciso apagar toda a string após certo caractere. Por exemplo: "[ABC12]Texto Qualquer" >>> "[ABC12]". No caso acima, deletar toda a string após o caractere "]" mas mantendo ele. Como proceder? Estou usando string.gsub
DNL291 Posted November 1, 2017 Posted November 1, 2017 local s = "[aabbcc]12345" print( s:sub( 1, s:find("]")) ) -- > [aabbcc] 1
Other Languages Moderators Lord Henry Posted November 1, 2017 Author Other Languages Moderators Posted November 1, 2017 Obrigado. Fiz assim e funcionou como eu queria. addCommandHandler ("limpa", function (thePlayer, cmd, theText) if theText then outputChatBox (theText:sub (1, theText:find("]")), thePlayer) end end) Por curiosidade, e se eu quiser remover todo o texto que estiver fora dos [ ] ? Remover o que estiver antes do [ e depois do ]
DNL291 Posted November 1, 2017 Posted November 1, 2017 Só usar a função string.sub e string.find ( pra encontrar os índices dos caracteres ). Off: Me lembro de ter 'quebrado a cabeça' usando funções de string e executando com o programa Lua pra separar certos números de um arquivo de texto, enfim, acabei nem finalizando isso, mas funcionou e deu pra aprender algumas coisas já que eu também explorei umas funções Lua que não faz parte do MTA.
Banex Posted November 2, 2017 Posted November 2, 2017 Outra alternativa, seria fazer isso usando a função string.gmatch addCommandHandler("limpa", function(thePlayer, cmd, theText) if theText then for word in theText:gmatch("%b[]") do outputChatBox(word, thePlayer) end end end)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now