Solved Splitting a String every 5 words into separate strings?

Discussion in 'Plugin Development' started by maved145, Apr 5, 2016.

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

    maved145

    So i'm getting a string from the config that is set by the player and i need to split the string every 5words.
    For example: "This is a example on splitting a string every 5 words" will output: "This is a example on", "splitting a string every 5", "words".

    At the moment i have got:

    Code:
    String[] parts = configString.split(" ");
    I have tried a few things with this from what i have found online but can't seem to get it right.

    This works if i want to split the string every two words but i need it for 5words.

    Code:
    String[] parts = configString.split("(?<!\\G\\w+)\\s");
    any help?
     
  2. Why not just split it with space and then use a stringbuilder and a loop to put them back together in a step of 5 entries in the array? No need for regex.

    "If you really care what it does then i can tell you [...]" seems to me a lil bit like spoonfeeding, but i guess this time its at least not that bad. Probably the wrong attitude tho :D

    For simple stuff like that always prefer not to use regex, because those pattern can be hard to read once you didn't work with them for a time or show your code to other developers. Algorithms are always understandable if written good.
     
  3. Offline

    mcdorli

    There was a flaw in the regex I posted earlier, this is how it should look like:

    (?<=\S+\s\S+\s\S+\s\S+\s\S+)\s

    Don't forget to replace \-s with \\, so they don't cause problems
     
    Last edited: Apr 5, 2016
    Lordloss likes this.
  4. Code:
    function read {
    int fi = 1
    int to = 2
    int tr = 3
    int fo = 4
    int fv = 5
    int a = 0
    String[] same
    
    String[] parts = configString.split(" ");
    same[a] = (parts[fi] + parts[to] + parts[tr] + parts[fo] + parts[fv])
    
    if  (parts.lenght == a * 5) {
    
    }else{
    int fi = 1 + 5
    int to = 2 + 5
    int tr = 3 + 5
    int fo = 4 + 5
    int fv = 5 + 5
    int a = a + 1
    read()
    }return
    }
    
    
     
  5. Offline

    maved145

    @mcdorli This works but splits it every word not every 5 words :/ any ideas?
     
  6. Offline

    WolfMage1

  7. Offline

    maved145

    @WolfMage1 I did look at that earlier but from what i understand from that it splits at every nth character, so i would have to know how many characters there are in each of my 5word segments correct? Not too sure.. :p
     
  8. Offline

    WolfMage1

    Did you try it?
     
  9. Offline

    maved145

    @WolfMage1 Yes, splits the string into separate parts of 5 characters :/
     
  10. Offline

    mcdorli

  11. Offline

    maved145

    @mcdorli Okay, this works for the first 5 words now but not every 5 words, after it has got the first 5 words in 1 part the rest of the string is separated by 1 word again. Thanks for your help :)
     
  12. Offline

    mcdorli

    Forgot the global tag

    (?<=\G\S+\s\S+\s\S+\s\S+\s\S+)\s

    There was a problem with the original 2 word version, it used \w (select an alphanumeric character) instead of \S (select every non-whitespace character), so if you had "Hello Tim & Tom", then it would have problems with the &, as it's not a word.
     
  13. Offline

    maved145

    @mcdorli Ehmm, don't know if i made a mistake while changing all the \ to \\ but it doesn't split anything at all now, just returns the full string.

    Code:
    String[] parts = string.split("(?<=\\G\\S+\\s\\S+\\s\\S+\\s\\S+\\s\\S+)\\s");
    Am i missing something?
     
    Last edited: Apr 5, 2016
  14. Offline

    mcdorli

    It should work, I check it when I get to my computer, where I tested this.
     
  15. Offline

    maved145

    @mcdorli Ok let me know. :)

    @mcdorli had time to check if it works? i have tried messing around with things and just can't seem to get it working with the global tag in the regex

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 6, 2016
  16. Offline

    mcdorli

    This works for me:
    Code:
    String s = "rbc coiewnc weiwo coiwnec piwencq pqmwd pqwndé qpowd bdiubec wuweuw dweondonwe woeidnwoend woenew";
    String[] ss = s.split("(?<=\\G\\S+\\s\\S+\\s\\S+\\s\\S+\\s\\S+)\\s");
    for (String sss : ss) {
        System.out.println(sss);
    }
    
    (Sorry for inconvenient names, I really didn't cared about that)
     
  17. Offline

    maved145

    @mcdorli That might be the problem, im adding each 5word section into a ArrayList by doing Arrays.asList(ss).. I'll try doing it with the for loop, thanks for help.

    @mcdorli Okay well i found the issue, seems to be that it doesn't want to do it to a string i get from my config but it will work on a string i create.. any ideas?

    Without the global tag it does work on the configString but only the first 5 words but with the global tag it doesn't split the configString but will work on a String i create in the IDE.
     
    Last edited: Apr 6, 2016
  18. Offline

    CraftCreeper6

    @maved145
    Do you get the string directly?

    for (thing : config.stringThing()){

    Or do you save the string first?

    string = config.stringThing();
    for (thing : string){

    Just re-read OP, save it as a normal string, not a string array.
     
  19. Offline

    maved145

    @CraftCreeper6 I set the String by doing String configString = getConfig().getString()... and then i split that string with the regex into a string[].
     
  20. Offline

    CraftCreeper6

    @maved145
    Do you split it with the correct regex like @mcdorli posted?

    Also, show your code.
     
  21. Offline

    maved145

    @CraftCreeper6 Never mind, think i have found the problem, The way im saving the string into the config is from arguments in a command and its adding a space to the start of every string, seems that the regex doesn't like that space at the start so i will make it so it saves without the space at the start and it should be good then :) i'll set to solved if this works, going to try it now.

    @mcdorli @CraftCreeper6 Yeah that was the problem. Thanks for the help.
     
    Last edited: Apr 6, 2016
  22. Offline

    I Al Istannen

    @maved145
    You might want to add some comments to remember what it does. If you don't and need to look at it after some time, I want to see your face. (although it is quite understandable, but I hope you get the point)
     
    maved145 likes this.
  23. Offline

    maved145

    @I Al Istannen Yeah will do, i tend to comment a lot of my stuff as it makes it look more organised to me and makes it easy for me when i'm looking for certain methods and functions :p
     
Thread Status:
Not open for further replies.

Share This Page