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.
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
Joined: Jun 11, 2003
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.
Joined: May 22, 2002
I heard that the regular expression is a good thing. No, it's super.
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.