StringList replaceAll?

Discussion in 'Spigot Plugin Development' started by OldTimes, May 3, 2015.

  1. I'm trying to replace %player% with the players name, however it's not replacing?

    Code:
    Code (Text):
    for (String list : ach.getData().getStringList(
                            achieve + "1.Commands")) {
                        list.replaceAll("%player%", p.getName());
                        Bukkit.getServer().dispatchCommand(
                                Bukkit.getServer().getConsoleSender(),
                                list.toString());
                    }
    It's getting it from the correct place in the YML file and there are no errors.
    Thanks!
     
  2. Just use replace() instead of replaceAll().
     
  3. @OldTimes first of all, use String#replace as String#replaceAll is for regular expressions. Secondly, replace (and replaceAll) do not modify the String (String are immutable after all), they return the modified String.
     
  4. gigosaurus

    Supporter

    The replace and replaceAll method returns the modified string rather than changing it - it is unable to modify the original string because strings are immutable.
    You should use replace instead of replaceAll as replaceAll is for regex.
    Code (Java):
    for (String list : ach.getData().getStringList(achieve + "1.Commands")) {
        Bukkit.getServer().dispatchCommand(
            Bukkit.getServer().getConsoleSender(),
            list.replace("%player%", p.getName()));
    }
    EDIT: I was too slow :p
     
  5. Use String#replace()