Jump to content

Apagar string após caractere


Recommended Posts

  • Other Languages Moderators
Posted

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

  • Other Languages Moderators
Posted

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 ]

Posted

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.

Posted

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)

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...