Solved Replacing one word to a list of strings?

Discussion in 'Spigot Plugin Development' started by 6u5t4v, Mar 24, 2020.

  1. The title is selfexplanatory. I want to replace one word such as {newlist} into a list of strings :)
     
  2. drives_a_ford

    Moderator

    Just because you know what you want, doesn't mean everybody else will know it as well.

    It really isn't clear what you want.
    How do you want to represent this list of strings exactly? Comma separated? Something else?

    If you want one word to be replaced by multiple words in a sequence you can use String#replace and String#join.

    If you want to do something else, you'll need to elaborate.
     
  3. Do you know what a list of strings is?, because it is typically represented like this:
    Code (Text):
    - "line 1"
    - "line 2"
    - "line 3"
    - "line 4"
    So I cant see how it can be represented another way.
     
  4. drives_a_ford

    Moderator

    You're then talking about YAML. Which you never mentioned. Hence:

    What is it you want to do exactly then?
    Change something {newlist} more something to something - "line 1"
    - "line 2"
    - "line 3"
    - "line 4" more something
    ?
    I doubt it.

    You need to specify what your goal is. I cannot read your mind.
     
  5. I have a list
    Code (Text):
    - "yeet"
    - "d sdfgs {newlisthere} asdjklfajsdlkæf"
    - "yeet"
    - "yeet"
    I want to replace {newlisthere} with a new list:
    Code (Text):
    - "yeet2"
    - "yeet2"
    - "yeet2"
    Which makes the final list look like
    Code (Text):
    - "yeet"
    - "yeet2"
    - "yeet2"
    - "yeet2"
    - "yeet"
    - "yeet"
     
    You see how I want to replace the one word ({newlisthere}) with another list of string ? :)
     
    #5 6u5t4v, Mar 24, 2020
    Last edited: Mar 24, 2020
  6. Create new list, like List<String> newList.
    Iterate your original list.
    If string equals your placeholder - use newList.addAll(replaceList), else just add this string to newList.
    Return newList.
     
  7. drives_a_ford

    Moderator

    If you're really talking about YAML, what "replacing one word with a list" would do is go from
    Code (YAML):
    - "1"
    - "{replacethis}"
    - "3"
    to
    Code (YAML):
    - "1"
    - - "replace1"
       - "replace2"
    - "3"
    Because you'd be replacing a list item with another list.


    So it sounds like what you really want to do is remove one element from a list and then add several others starting from where the element originally was.

    You first want to figure out the index at which your to-be-replaced string is in the list (you can use List#indexOf if you don't previously know this).
    You then want to List#remove the element at said index.
    Finally, you can List#addAll and pass the index of the item that you removed to add all elements in the replacement list starting from this index (and push the rest of the elements further back).

    EDIT:
    No need to create a new list. This can easily be done within the same list like I described above.
     
    • Like Like x 1
  8. like so?
    Code (Text):
    int modeLine = Config.harvesterhoelore.indexOf("{mode_lore}");
            Config.harvesterhoelore.remove(modeLine);
            Config.harvesterhoelore.addAll(modeLine, Config.harvesterhoelore_mode1);
     
  9. drives_a_ford

    Moderator

    If harvesterhoelore is the list in which you want to "replace" the "{mode_lore}" element with all the elements in harvesterhoelore_mode1, then this should do the trick.

    However, this limited amount of code already shows classic signs of static abuse and lack of encapsulation. I suggest you read Beginner Programming Mistakes and Why You're Making Them.
     
  10. Thanks for the help.

    This limited amount of code explains nothing of how I code and no there is no statis abuse here. I rarely use static. So dont be that guy who point others towards programming guides when you know nothing.
     
  11. Strahan

    Benefactor

    There are many ways one can represent a list of strings. MANY. Just because you don't know them, doesn't mean they don't exist ;) Now had you said a List of strings, that would narrow the field significantly.
     
  12. First of all I did say list of strings, and I am not sure what other ways a list of strings can be represented other than the one I made, or atleast cant think of any. Other then that If you didnt notice it, I also mentioned that "it is typically shown like this".
     
  13. Strahan

    Benefactor

    Uhh.. yea:
    ...and
    Yea, after drives_a_ford pointed out we need more detail. The whole point was that your initial post could've used more details. Logically one would assume you mean a List<String> but as already mentioned there are other ways to represent a collection of strings.
     
  14. I dont see what you are trying to say. I did mean a List<String>.
     
  15. Choco

    Moderator

    Just be more descriptive. It's not that difficult. The OP had absolutely 0 information we could work off of. No examples of what you wanted, no description or clarification, not even a mention of it being a YAML file. The first handful of replies in this thread were inquiring what you meant because we cannot read minds. Provide as much detail as possible. More than you think is even necessary. Only then can we answer your question more effectively.

    and yes, by the way - the code you showed above made use of static as an access modifier. Read the linked thread, it will help clarify as to why it's static abuse and why you should avoid doing so in the future.
     
    • Like Like x 1