user2050089 user2050089 - 2 months ago 5
Java Question

How to split to parse below string "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy"

I have a String,

String data = "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy";

I want to split it as below,


String[] array = data.split(?);

What regex can be used to get the exact output?


It's not pretty, and probably not the best solution, but it does what you want. So preferably I would like someone else to post a better answer or you'll have to wait for me to write one.

public class Test {
    public static void main(String[] args) {
        String data = "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy";
        String[] array = data.split(",");
        for(String s : array) {
            if(s.contains("=")) {
                System.out.print("\n" + s);
            } else {
                System.out.print("," + s);

I know that this prints an empty line first and if you really want to remove this then you can add an if-statement for that.