Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

how to deal with this string?

 
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.
 
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.
 
Wanderer
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic