Sunday, 15 September 2013

java - How can I split this string on comma and quotation mark? -



java - How can I split this string on comma and quotation mark? -

this question has reply here:

java: splitting comma-separated string ignoring commas in quotes 9 answers

i read text file , in string like:

class="lang-none prettyprint-override">;onor oyj,native,uor oyj1,"uponor oyj, ubs, innovation ab",39639d4d26:-21f7;

i need split string this:

class="lang-none prettyprint-override">'onor oyj', 'native', 'uor oyj1', 'uponor oyj, ubs, innovation ab', '39639d4d26:-21f7', ';'

how can this?

something like:

string str = ";onor oyj,native,uor oyj1,\"uponor oyj, ubs, innovation ab\",39639d4d26:-21f7;"; char[] c = str.tochararray(); boolean inquote = false; (int = 0; < c.length; i++) { if (c[i] == ',' && !inquote) c[i] = '\01'; // value doesn't appear in string if (c[i] == '"') inquote = !inquote; } string[] arr2 = string.valueof(c).split("\\01"); (string s: arr2) { system.out.println(s); }

outputs:

;onor oyj native uor oyj1 "uponor oyj, ubs, innovation ab" 39639d4d26:-21f7;

replace string[] arr2 = string.valueof(c).split("\\01"); string[] arr2 = string.valueof(c).replaceall("\"", "").split("\\01"); remove quotes.

java split

No comments:

Post a Comment