commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacek Furmankiewicz <jace...@gmail.com>
Subject StrTokenizer not handling quotes correctly?
Date Wed, 15 Apr 2009 12:39:57 GMT
I am trying to use StrTokenizer for some parsing and I am probably not using
it correctly.

Let's say I have this string:

11"a,b"11,22"c,d"22"

I would like to split it by the comma ",", but ignoring any commas embedded
in quotes. I try this:

        String test = "11\"a,b\"11,22\"c,d\"22";
        StrTokenizer str = new StrTokenizer(test,',','"');
        String[] tokens = str.getTokenArray();

        for(String t: tokens) {
            System.out.println(t);
        }

and expect to have two strings print out:

11"a,b"11
22"c,d"22

but instead I get 4 :

11"a
b"11
22"c
d"22

It seems the tokenizer is splitting on the comma, even if it is embedded in
quotes.

I tried different options on the StrTokenizer, but not been able to get it
to work correctly.

Any idea as to what am I doing wrong? Using latest version 2.4.

Thanks, Jacek

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message