Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to deal with this string?

 
greg norman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
greg norman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I heard that the regular expression is a good thing.
No, it's super.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic