1

I have a string with value - String sData = "abc|def|\"de|er\"|123"; and I will need to split it with delimiter - "|". In this case, my expected result will be

abc
def
"de|er"
123

Below is my code

String sData = "abc|def|\"de|er\"|123";
    String[] aSplit = sData.split(sDelimiter);

    for(String s : aSplit) {
        System.out.println(s);
    }

But it actually comes out the below result

abc
def
"de
er"
123

I have tried with this pattern - String sData = "abc|def|\"de\\|er\"|123"; but it's still not returning my expected result.

Any idea how can I achieve my expected result?

chris
  • 29
  • 5

1 Answers1

0

This worked for me:

String sData = "abc|def|\"de|er\"|123";
    String[] aSplit = sData.split("\\|");

        for(int i = 0; i < aSplit.length; i++) {
            if(aSplit[i].startsWith("\"")) {
                if(aSplit[i+1].endsWith("\"")) {
                    aSplit[i] = aSplit[i] + "|" + aSplit[i+1];
                    aSplit[i+1] = "";
                }
            }
        }

    for(String s : aSplit) {
        if(!s.equals(""))
        System.out.println(s);
    }

The output:

abc
def
"de|er"
123
Cardinal System
  • 2,749
  • 3
  • 21
  • 42