aspose file tools*
The moose likes Java in General and the fly likes how to deal with this string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to deal with this string?" Watch "how to deal with this string?" New topic
Author

how to deal with this string?

greg norman
Greenhorn

Joined: Jun 11, 2003
Posts: 19
I have a string with is retrieved from database.
The string looks like "1,2,15,23,232", which is a type of SET in the database.
In the Java program, I need to obtain each separate index from the string "1,2,15,23,232". Can any one let me know how to do that EFFICIENTLY?
Thanks a lot.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336


EFFICIENTLY
You could also use a StringTokenizer and it's probably more efficient then using regex.
Maybe JRY can help us with that :roll:
[ July 07, 2003: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
greg norman
Greenhorn

Joined: Jun 11, 2003
Posts: 19
Thanks for help.
Looks like using the "split" method is a way to go. Just found this was mentioned in Java 1.4.2 documention.

"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

I heard that the regular expression is a good thing.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
I heard that the regular expression is a good thing.
No, it's super.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You could also use a StringTokenizer and it's probably more efficient then using regex.
Maybe JRY can help us with that :roll:

Actually I think the only thing that might be slower about regex is the time it takes to compile the pattern. (Which is something that's done every time if you use String's split() method.) If the same sort of split() will be repeated many times and you want to improve performance, try something like:

The goal here was to get the compile() step to happen only once (since it's always the same pattern), while the rest of the split() happens as many times as needed.
In general though I agree that it's usually preferable to just forget about StringTokenizer and learn to use regex well.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to deal with this string?