Splitting a String, editing it, then putting it back together.

Discussion in 'Plugin Development' started by Niknea, Jul 2, 2014.

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

    Niknea

    Hello everyone,

    I'm currently creating a plugin, with a chat feature. I would like to split a string (the user's message), loop and find a specific part of the split string(s), edit that part, and put it all back together. Any ideas how to do this?

    Thanks,

    - Niknea
     
  2. Offline

    teej107

  3. Offline

    unon1100

    Enhanced for loops. I do some things in this tutorial that should show you how to put arrays of strings together.

    Use String.split(<regex string>); to create a string array.

    For instance: "Lol,Why,xD".split(","); would return {"Lol", "Why", "xD"};

    Be careful. The parameter input is a regex (regular expression)
    This means that some characters will have a special meaning.

    If you do "Lol|Why|xD".split("|"); it won't work, as pipes have a special meaning in regular expressions. You would need to do split("\\|); for it to work.

    Now that you know how to split string and put them back together, you can use this information and combine it with creative thinking to do whatever you need to do with splitting and fusing strings.
     
  4. Offline

    Niknea

    teej107 unon1100 I know how to split the string, however how would I go along editing a specific part of it which I don't know how to do. Along with looping through all the split parts, and putting it back together (at least I don't see it in unon1100 posts )
     
  5. Offline

    teej107

    while looping through your split, check to see if the string equals/endswith/etc. then modify the string to your liking. Modified or not, then add the string to a new string using StringBuilder's append method or just by creating a new string and using + operator.
     
  6. Offline

    xize

    Niknea

    you may could use a subString when looping/creating and then use .replace() in combination with the length but thats is maybe over complicated, could I ask what needs to be replaced and is it a dynamic word or a sentence replacement?
     
  7. Code:Java
    1. String[] splitted = "Hi,my,name,is,SPONGEBOB".split(",");
    2. StringBuilder sb = new StringBuilder();
    3. for (String string : splitted) {
    4. if (string.equalsIgnoreCase("spongebob"))
    5. string = "FisheyLP";
    6.  
    7. sb.append(string).append(",");
    8. }
    9. String finalText = sb.toString.subString(sb.length() - 2);
    10.  
     
  8. Offline

    Niknea

    Alright so I've managed to it it all, however I can't figure out how to put the new string into an actual string object, and send it to the player. Any ideas?
     
  9. Offline

    Zupsub

    Use a StringBuilder, loop over the array, append the string, then get the whole string with StringBuilder#toString();
     
  10. Offline

    teej107

  11. Offline

    Necrodoom

    Niknea paste current code.
     
Thread Status:
Not open for further replies.

Share This Page