3

This maybe has been asked before, if so, please just refer the URL.

I have some string that I want to split, example:

"TEXTVALUE";NUMBER;"TEXTVALUE";DOUBLE;DATE;"TEXTVALUE"

But, after some line reads, in the original data I got something like this:

"TEXTVALUE;NUMBER;"TEXTVALUE;TEXTVALUE";DOUBLE;DATE;"TEXTVALUE"

Notice the second text data, it has the splitter character inside the quotes. I've been trying to work around this issue with a pattern, but I'm not sure how to achieve this.

Maybe something like:

string.split("["+character+"&&[^\"?[\\w*|"+character+"*]\"?]]");

or

string.split("["+character+"]&&[^\".*\"]");

This is the output I'm trying to achieve:

"TEXTVALUE"
NUMBER
"TEXTVALUE;TEXVALUE"
DOUBLE
DATE
"TEXVALUE"
Wolfchamane
  • 255
  • 1
  • 5
  • 23
  • Is there any guarantee that it's just `".."` and not like `"" .. "` or more nested / uneven (escaped?) quotes? – zapl Aug 02 '13 at 08:16
  • 1
    possible duplicate of [Java: splitting a comma-separated string but ignoring commas in quotes](http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes) – Peter Lang Aug 02 '13 at 08:21
  • Yep, completely sure that text values are quoted as: "value" – Wolfchamane Aug 02 '13 at 08:21
  • describe here your expected value from `"TEXTVALUE;NUMBER;"TEXTVALUE;TEXTVALUE";DOUBLE;DATE;"TEXTVALUE"`, that will help a lot – Angga Aug 02 '13 at 08:46

3 Answers3

0

class SplitDemo

{

public static void main(String args[])

{

    String str="\"TEXTVALUE\";NUMBER;\"TEXTVALUE\";DOUBLE;DATE;\"TEXTVALUE\"";

    String newSplit[] = str.split(";");

    for(int i=0;i<newSplit.length;i++)

    {

        System.out.println(newSplit[i]);

    }
}

}

output

"TEXTVALUE"

NUMBER

"TEXTVALUE"

DOUBLE

DATE

"TEXTVALUE"

0

Split by your delimiter only when an even number of quotes appear in he rest of the input:

String[] parts = str.split(";(?=(([^\"]*\"){2})*[^\"]*$)");

See a live demo on IDEOne.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

A simple example of how to split the text can be as follows:

public class StringSplit {
public static void main(String[] args) {
    String s = "\"TEXTVALUE\";NUMBER;\"TEXT;VALUE\";DOUBLE;DATE;\"TEXTVALUE\"";
    String[] strSplit = s.split(";");
    /*
     * first, we split the string after the ";" character then, we try to
     * evaluate and see if there where any ";" characters in our text
     * fields, if they where, we concatenate the strings such that to obtain
     * only one
     */
    StringBuilder buf = new StringBuilder();

    for (int i = 0; i < strSplit.length; i++) {
        int count = 0;
        if (strSplit[i].charAt(0) == '\"') {
            count++;
        }
        if (strSplit[i].charAt(strSplit[i].length() - 1) == '\"') {
            count++;
        }
        if (count % 2 == 1) {
            buf.append(strSplit[i]);
            buf.append(";");
            buf.append(strSplit[i + 1]);
            buf.append("\n");
            i++;
        } else {
            buf.append(strSplit[i]);
            buf.append("\n");
        }
    }
    System.out.println(buf.toString());
}

}

The result looks like:

"TEXTVALUE"
NUMBER
"TEXT;VALUE"
DOUBLE
DATE
"TEXTVALUE"

miha1908
  • 157
  • 1
  • 1
  • 6