Jump to content

Replace char in nickname


DakiLLa

Recommended Posts

hi there. i got one problem:

i have next:

addCommandHandler( "test", 
  function ( player ) 
    local name = getPlayerName( player ); 
    local i = string.find( name, "|", 1, plain ); 
    if i then 
      outputChatBox( "You got a char '|' in "..i.." index" ); 
    end; 
  end 
); 

if i have a '|' symbol in playername, then how can i replace it, if i know it's index position?

Link to comment

You can just use string.gsub to replace any occurrences of | if you want and you don't need to have the index position.

http://lua-users.org/wiki/StringLibraryTutorial

  
addCommandHandler("test", 
    function (player) 
        local nick = getPlayerName(player) 
        local newnick = string.gsub(nick,"|","") -- searches for | and replaces with nothing, eg removes them 
        setPlayerName(player, newnick) 
    end 
) 
  

Took the function names and syntax from memory so they might have some faults. But just for you to see.

EDIT: Also in your code, the last argument of string.find has to be boolean (true or false), you just have 'plain' (eg a nil value)

Link to comment

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