Java: splitting a comma-separated string but ignoring commas in quotes

前端 未结 11 1718
广开言路
广开言路 2020-11-21 05:16

I have a string vaguely like this:

foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"

that I want to split by commas -- but I need to igno

11条回答
  •  半阙折子戏
    2020-11-21 06:07

    While I do like regular expressions in general, for this kind of state-dependent tokenization I believe a simple parser (which in this case is much simpler than that word might make it sound) is probably a cleaner solution, in particular with regards to maintainability, e.g.:

    String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
    List result = new ArrayList();
    int start = 0;
    boolean inQuotes = false;
    for (int current = 0; current < input.length(); current++) {
        if (input.charAt(current) == '\"') inQuotes = !inQuotes; // toggle state
        else if (input.charAt(current) == ',' && !inQuotes) {
            result.add(input.substring(start, current));
            start = current + 1;
        }
    }
    result.add(input.substring(start));
    

    If you don't care about preserving the commas inside the quotes you could simplify this approach (no handling of start index, no last character special case) by replacing your commas in quotes by something else and then split at commas:

    String input = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
    StringBuilder builder = new StringBuilder(input);
    boolean inQuotes = false;
    for (int currentIndex = 0; currentIndex < builder.length(); currentIndex++) {
        char currentChar = builder.charAt(currentIndex);
        if (currentChar == '\"') inQuotes = !inQuotes; // toggle state
        if (currentChar == ',' && inQuotes) {
            builder.setCharAt(currentIndex, ';'); // or '♡', and replace later
        }
    }
    List result = Arrays.asList(builder.toString().split(","));
    

提交回复
热议问题