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

    How to split string ignoring stuff in quotes

    807588
      Hello,
      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???

      Thanks!
        • 2. Re: How to split string ignoring stuff in quotes
          796365
          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\"";
                  System.out.println(s);
                  String[] sa = s.split("\",");
                  for (String x : sa)
                  {
                      x = x.trim();
                      x = x.endsWith("\"") ? x : x + "\"";
                      System.out.println(x);
                  }
              }
          }{code}