2 Replies Latest reply on Jul 2, 2009 4:19 AM by 796365

    How to split string ignoring stuff in quotes

      I was wondering if there was a simple way to split a set of comma-separated strings like this:
      Frakie said: "This is first", I said this: "This is second", She replied "Me, myself, and I"

      In essense, the harder part is to ignore the commas between quotes. I usually just pull out stuff in quotes, and replace quoted stuff with an index linking to the quotes:
      {0}, I said this: {1}, Shre plied {2}

      Now I can freely parse by commas and put the quoted strings back.

      Is there a better way to do this???

        • 2. Re: How to split string ignoring stuff in quotes
          Better ?
          public class Test
              public static void main(String[] args)
                  String s =
                      "Frakie said: \"This is first\", I said this: \"This is second\", She replied \"Me, myself, and I\"";
                  String[] sa = s.split("\",");
                  for (String x : sa)
                      x = x.trim();
                      x = x.endsWith("\"") ? x : x + "\"";