Replaceall in a List of string

Discussion in 'Plugin Development' started by Innofly, Mar 31, 2014.

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


    Is there a way to do .replaceALL into a list of string?

    1. List<String> help = plugin.getConfig().getStringList("help");

    I already tried
    1. help = help.replaceALL("&", "ยง");
    but its not working.

  2. Offline


    You would want to loop through the list and use replaceAll on each String. However, for your example, there is already a Bukkit method to convert ampersands to section symbols for the purposes of color (if I am mistaken with your intent, feel free to correct me :) )
    ChatColor.translateAlternateColorCodes((char) '&', yourStringObject);
  3. Offline



    I tried what you said but its not valid with a string list. :(
  4. Offline


    I know it probably seems a bit nitpicky, but you do not need to cast '&' to char. It's already a character literal.
  5. Offline


  6. Offline


    Iterate over the list of strings. Just a warning, Strings are passed immutable in Java so you will need to write them back to the arraylist.

    2. for (int i = 0; i < list.size(); i++) {
    3. String s = list.get(i);
    4. // Manipulate s
    5. list.set(i, s);
    6. }
  7. Offline


    Gonna have to correct you there. Strings, like any other object, are passed by reference in Java. The reason you need to write them back in is the fact that Strings are immutable, thus none of the methods you perform on a String will ever mutate/change the String object itself. Some of the methods (like replaceAll), return a new String object with the modifications applied. That is why you have to reassign the reference variable.

    EDIT: Embarassing. Reading a little further into it, I've learned that objects are passed AS references, and that those references are passed BY value. Objects are never really passed at all. Only their value. Read more about it here
  8. Offline


    Rocoty I know, I just confused it with the primitives for a second there. I should probably not post at 5AM
Thread Status:
Not open for further replies.

Share This Page