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

    Innofly

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

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


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

    Thanks.
     
  2. Offline

    SGrayMe

    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 :) )
    Code:
    ChatColor.translateAlternateColorCodes((char) '&', yourStringObject);
     
  3. Offline

    Innofly

    SGrayMe

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

    amhokies

    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

    SGrayMe

  6. Offline

    coasterman10

    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.

    Code:java
    1.  
    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.  
     
  7. Offline

    Rocoty

    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 references...by value. Read more about it here
     
  8. Offline

    coasterman10

    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