Solved Replace text in string

Discussion in 'Plugin Development' started by KarimAKL, Apr 29, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    KarimAKL

    Hey, i want to replace some text in a string like {player} with the name of a player so that you can use {player} in config but when i try doing this:
    Code:Java
    1.  
    2. p.sendMessage(ChatColorUtil.chat(plugin.messages.getString("whatever").replace("{world}", world).replace("{player}", target)));
    3.  

    I get the following error at the second ".replace" part.
    Code:
    The method replace(char, char) in the type String is not applicable for the arguments (String, Player)
    
    I have used this multiple times before but it doesn't seem to work now, what am i doing wrong? :/
    EDIT: I think i understand the error, is it because "target" is a player and i need to turn it into a string somehow? If so then how would i do that?
    EDIT2: When i said i had done this multiple times before i didn't think it was because it was a player this time. (all the other times have been text and not players)
     
    Last edited by a moderator: Apr 29, 2018
  2. try
    Code:
    string = string.replace("anything" , target)
    p.sendMessage(string)
    or if you want colors
    Code:
    p.sendMessage(ChatColor.translateAlternateColorCodes('&',string)));
     
  3. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL did you try getName or getDisplayName on the player object?
     
  4. Offline

    KarimAKL

    @TGamingStudio I'll try that, thanks. :) Also my "ChatColorUtil" does that already. :p @timtower Actually no, i didn't even think of that. :p I'll try that aswell, thanks. :)
    EDIT: I just tried both and @TGamingStudio i couldn't get your method to work. :/ And @timtower i tried adding the ".getName" on the target and it worked without errors, thanks alot both of you for helping. :D
     
    Last edited by a moderator: Apr 29, 2018
Thread Status:
Not open for further replies.

Share This Page