Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2003

Tip: Looking for answers? Try searching our database.

StringTokenizer Question?

Thread view: 
Jim Crowell - 20 Aug 2003 16:32 GMT
I have a method to unpack a String via a given String delimiter. It works
fine but it used the 'String' Class 'indexOf' and 'substring' Methods within
a 'for' loop.

To make this method more efficient I tried to substitute the
'StringTokenizer' Class. Easy enough but it looks like the following input
String returns 3 token where I would expect 4 tokens:

"AA,BB,,1," with delimiter = ","

Also, the following String returns 3 Tokens where I would expect 4:

",BB,CC,1," with delimiter = ","

With my code I use an 'if' condition before I unpack to ensure that the
string ends with the delimiter but for 'StringTokenizer' I see that the
following input String returns 4 tokens:

"AA,BB,CC,1" with delimiter = ","

I assume that a token must have 1 or more characters and that I must return
to my original code.

Is there a way to make 'StringTokenizer' recognize consecutive delimiters
and return a null String [e.g. ""] as a token?

Thanks

Jim.
Andy Fish - 20 Aug 2003 18:29 GMT
I can't see any way to do it with StringTokenizer. However, you could use
String.split()

> I have a method to unpack a String via a given String delimiter. It works
> fine but it used the 'String' Class 'indexOf' and 'substring' Methods within
[quoted text clipped - 25 lines]
>
> Jim.
Tor Iver Wilhelmsen - 20 Aug 2003 19:02 GMT
> To make this method more efficient I tried to substitute the
> 'StringTokenizer' Class. Easy enough but it looks like the following input
> String returns 3 token where I would expect 4 tokens:
>
> "AA,BB,,1," with delimiter = ","

Thet's because you haven't told it to return "empty" tokens - check
the constructors.

> Also, the following String returns 3 Tokens where I would expect 4:
>
> ",BB,CC,1," with delimiter = ","

Same thing, except you should expect five. :)
Roedy Green - 20 Aug 2003 19:17 GMT
>Also, the following String returns 3 Tokens where I would expect 4:

StringTokenizer(String str, String delim, boolean returnDelims)

Try setting the last parameter of the constructor to true.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
samspade - 20 Aug 2003 22:36 GMT
> I have a method to unpack a String via a given String delimiter. It
> works fine but it used the 'String' Class 'indexOf' and 'substring'
> Methods within a 'for' loop.

> To make this method more efficient I tried to substitute the
> 'StringTokenizer' Class. Easy enough but it looks like the following
> input
> String returns 3 token where I would expect 4 tokens:

> "AA,BB,,1," with delimiter = ","

> Also, the following String returns 3 Tokens where I would expect 4:

> ",BB,CC,1," with delimiter = ","

> With my code I use an 'if' condition before I unpack to ensure that
> the string ends with the delimiter but for 'StringTokenizer' I see
> that the following input String returns 4 tokens:

> "AA,BB,CC,1" with delimiter = ","

> I assume that a token must have 1 or more characters and that I must
> return to my original code.

> Is there a way to make 'StringTokenizer' recognize consecutive
> delimiters and return a null String [e.g. ""] as a token?

If you use string.split(regex) then it will return the empty matches for
you. I.e, you'd get 4 from your first example.

StringTokenizer is (almost) deprecated now for most uses.
John C. Bollinger - 21 Aug 2003 16:17 GMT
> If you use string.split(regex) then it will return the empty matches for
> you. I.e, you'd get 4 from your first example.
>
> StringTokenizer is (almost) deprecated now for most uses.

Really?  That's news to me.  I am perfectly comfortable with Java 1.4
regexes, but I prefer StringTokenizer for those tasks to which it is
suited.  In practice, StringTokenizer is suited to a substantial number
of the most frequently occurring String parsing tasks.  Regexes are more
powerful, but also heavier, and their API is more complex.

John Bollinger
jobollin@idniana.edu
Jim Crowell - 23 Aug 2003 15:28 GMT
> Is there a way to make 'StringTokenizer' recognize consecutive delimiters
> and return a null String [e.g. ""] as a token?

Thanks for the many responses. In my original post I neglected to
state that I am still using Java 1.3.1.

Therefore, I believe I can not use String.split(Regex) yet.

To be honest, I did not know about 'Regex' until I received your
responses. I have since read a tutorial and look forward to using it
somewhere in my App.

I am avoiding using Java 1.4.1 because whenever I go to a new upgrade
I have had to make many changes to my many existing code and I can not
afford the time right now.

This is most likely do to poor programming practices on my part since
I think that a new version should be upword compatable.

Thanks again,
Jim
Roedy Green - 23 Aug 2003 19:28 GMT
>Therefore, I believe I can not use String.split(Regex) yet.

For a quick introduction to what Regexes can do for you, see
http://mindprod.com/jgloss/regex.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Stephen Ostermiller - 23 Aug 2003 16:06 GMT
I have three classes that will help you out:

1)  A StringTokenizer class that has the ability to return empty
tokens:
http://ostermiller.org/utils/StringTokenizer.html

2) A StringHelper class with a split method that does not use regular
expressions:
http://ostermiller.org/utils/StringHelper.html

3) A Complete Comma Separated Value (CSV) parsing class that you could
use to parse a string:
http://ostermiller.org/utils/CSV.html

Stephen


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.