Article View: borland.public.jbuilder.java.language
Article #8683Re: Can you use a String as a delimeter in StringTokenizer
From: Paul Furbacher [TeamB]
Date: Tue, 06 Mar 2001 19:36
Date: Tue, 06 Mar 2001 19:36
44 lines
2678 bytes
2678 bytes
Alex Colic wrote:
>
> Hi, here is my complete code. In my previous example I used "!" as the
> delimiter . I meant "Category!". I could not get it to parse correctly with
> the latter delimiter so I tried the former.
>
> String text="Category!Commodity Code";
> String strCategoryPreAppend="Category!";
> StringTokenizer token=new StringTokenizer(text,
> strCategoryPreAppend);
>
> System.out.println("Count: " + token.countTokens());
> while(token.hasMoreElements())
> {
> System.out.println("Token: " + token.nextElement());
> }
>
> Count: 4
> Token: mm
> Token: di
> Token:
> Token: d
>
> What I expect is two tokens: "Category" and "CommodityCode."
If you can drill down into the StringTokenizer class,
look at the scanToken() method. You'll notice that it
decomposes the "str" one character at a time from a
starting position. It then asks, "is this character in
the "delimiters" by checking for an indexOf() >= 0.
So, the approach taken by StringTokenizer to get tokens
is just about backwards from the way you (and many others)
think it should be working (i.e., looking for the string
you give as the delimiters as a substring of the source
string).
If you use the "!" as a delimiter, you can look for "Category"
token in a loop. When you find it, grab the next token and
then break out of the loop.
Paul Furbacher (TeamB)
- Please send responses to newsgroups only. Thank you.
Technical Details
Message-ID:
<3AA53C54.39ED719B@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!dispose.news.demon.net!demon!shale.ftech.net!news.ftech.net!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!24.9.87.144References:
<9810gn$fl1@bornews.inprise.com> <MPG.150e8591d5d31fe998a014@forums.borland.com> <982rcl$n9q4@bornews.inprise.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,22501d0a26c51927
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-06 11:46:03 PST
Message-ID: <3AA53C54.39ED719B@bilbo.bio.purdue.edu>
Date: Tue, 06 Mar 2001 14:36:52 -0500
From: "Paul Furbacher [TeamB]" <pf@bilbo.bio.purdue.edu>
Reply-To: pf@bilbo.bio.purdue.edu
Organization: Reverse Alchemy, Inc.
X-Mailer: Mozilla 4.7C-CCK-MCD {C-UDP; EBM-APPLE} (Macintosh; I; PPC)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: Can you use a String as a delimeter in StringTokenizer
References: <9810gn$fl1@bornews.inprise.com> <MPG.150e8591d5d31fe998a014@forums.borland.com> <982rcl$n9q4@bornews.inprise.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.9.87.144
X-Trace: 6 Mar 2001 11:36:19 -0800, 24.9.87.144
Lines: 44
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!dispose.news.demon.net!demon!shale.ftech.net!news.ftech.net!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!bornews.borland.com!dnews!24.9.87.144