Jump to content

string.gsub Problem


Albinix

Recommended Posts

Posted

Hey I have a problem with "string.gsub"

tag = "-|TG|-" 
name = "-|TG|-Albinix" 
  
subtag = string.gsub(name,tag,"") 
  
outputChatBox(subtag)   -- outputs -> "|-Albinix"  

Why does it not delete the whole tag?

  • Moderators
Posted

Yeah that's strange, because I doesn't see any errors :shock:

Maybe try this:

function test( thePlayer, commandName ) 
    local tag = "-|TG|-" 
    local name = "-|TG|-Albinix" 
    local subtag = string.gsub(tostring(name),tostring(tag),"") 
    outputChatBox( tostring( subtag ) ) 
end 
addCommandHandler("test", test, false, false ) 

But it's almost the same :|

Posted

Try this

  
function test( thePlayer, commandName ) 
    local tag = "-%|TG|%-" 
    local name = "-|TG|-Albinix" 
    local subtag = string.gsub(tostring(name),tostring(tag),"") 
    outputChatBox( tostring( subtag ) ) 
end 
addCommandHandler("test", test, false, false ) 
  

  • Discord Moderators
Posted

It is due to the magic characters in LUA: ( ) . % + - * ? [ ^ $

These characters have special meanings when used, and they change your string

Cadu12's method works because he escapes the magic character - in your tag, by using %.

For string.find, you can set the fourth argument to true and it will disregard string magic

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...