I separate the strings based on commas." "Can't you let me ignore the commas inside?"

Asked 2 years ago, Updated 2 years ago, 40 views

For example, foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy" If you have a string like this

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

I'm going to split it up by commas, but I want to ignore the commas in double quotes. How can we do that?

string java comma

2022-09-22 10:18

1 Answers

public class Main { 
    public static void main(String[] args) {
        String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
        String[] tokens = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        for(String t : tokens) {
            System.out.println("> "+t);
        }
    }
}

If you do this,

Output result:

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

It comes out like this.


2022-09-22 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.