Solved Help with the String.split method

Discussion in 'Spigot Plugin Development' started by Nemo_64, Jan 6, 2020.

  1. Is there any way to not split a string when there is a \ before the place to split?

    I explain myself

    Giving the string "abc-def-ghi" if I use the split("-") I'll get ["abc","def","ghi"]
    But if I have the string "abc\-def-ghi" and I use the split("-") I'll get ["abc\","def","ghi"] but I want to get ["abc-def","ghi"]

    Is there any way to do this?
     
    #1 Nemo_64, Jan 6, 2020
    Last edited: Jan 6, 2020
  2. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    You can replace the "\" by targetting the symbol with an additional "\". So .replace("\\","")
     
    • Useful Useful x 2
  3. And I would have been like "write your own parser"... but this is obviously a smart solution
     
    • Like Like x 1
  4. Agreed. However, you'd want .replace("\\-", "") to get rid of the hyphen as well, per OP's example. Then proceed to split.

    Edit: OP changed initial post from "I want to get ["abcdef","ghi"]" to "I want to get ["abc-def","ghi"]"
     
    #4 PikaMug, Jan 6, 2020
    Last edited: Jan 6, 2020
  5. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    Every solutions has it's pros and cons :)
     
    • Friendly Friendly x 1
  6. Sorry, I misspelled something, I want the result to be ["abc-def","ghi"] not ["abcdef","ghi"]
    However thanks for the advise!
    I have tried it but I can't get it to work, I don't know if I missunderstood
    When I use
    Code (Java):
            String inicial = "abc\\-def-ghi";
            String[] data = inicial.split("\\-");
            for (String a : data)
                System.out.println(a);
    I get
    Code (Text):
    abc\
    def
    ghi
    And I want to get
    Code (Text):
    abc-def
    ghi
     
  7. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    So what if you replace it for each entry?
    Code (Java):

    a.replace('-',"");
     
     
  8. no no no. the initial string was fine how it is.
    \ is an escape character. however, in java, it does not default to an escape character as it does not detect a special character after it.
    so you have to escape the escape character to escape - which is not a standard special character
    initial would be abc\-def-ghi
    .split would be \\-

    /e actually i might be braindead. might be backwards. if that doesnt work, try having \\- in your initial, and just having - in your split
     
  9. Since String.split supports regex, you could use a negative lookbehind. Not tested, but theoretically:
    Code (Java):
    string.split("(?<!\\)\-");
    You may need more backslashes since they might be parsed twice?

    Edit: this basically means if we find "-" that does not have a backslash before it, split here. You'll have to manually remove the backslash out of the resulting array though.
     
    • Winner Winner x 1
  10. Didn't work but may be the way to go, where can I find documentation?
     
  11. splitting
    Code (Java):
    "abc\\-def-ghi"
    with
    Code (Java):
    "(?<!\\\\)" + Pattern.quote("-")
    returns
    Code (Text):
    [abc\-def, ghi]
     
    • Winner Winner x 1
  12. It worked, where can I lear this for future ocasions?
     
  13. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    but "-" is not a special character.

    so can't you just replace it by using:
    Code (Text):
    a.replace("\\-","");
     
  14. My time to shine has come

    Code (Java):
    boolean ignoreHyphen = false;
    StringBuilder builder = new StringBuilder();
    final List<String> retStrings = new ArrayList<>();
    for (char c: s.toCharArray()) {
        if (c == '\\') {
            ignoreHyphen = true;
        } else if (c == '-' && !ignoreHyphen) {
            if (builder.length() <= 0) continue;
            retStrings.add(builder.toString());
            builder = new StringBuilder();
        } else {
            builder.append(c);
            ignoreHyphen = false;
       }
    }
     if (builder.length() > 0) retStrings.add(builder.toString());
     
    • Winner Winner x 1
  15. Wow you really did it, impresive.
    And it works, i'm going to use it
     
  16. thats what i'd think, but google says theres an issue with that. but i tested it and thats correct. no need to quote -. same result

    google
     
    • Agree Agree x 1
  17. Thx :D
    It's still better to use regexes (regular expression, simply search on google) since those are waaay more flexible (also a little bit slower but in almost all cases that's not noticeable)
     
  18. Thanks everyone
     
    • Friendly Friendly x 1
  19. Untill I understand them completly I'll use your code, ones I learn them I'll move on
     
  20. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    \\ is also regex. \ is escaping a character, \ is the character to be escaped.