• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help With Parsing a String Please  RSS feed

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have this string of stock data (the double quotes and commas are included in the string):
"IBM",124.09,+1.28,"2/11/2010"

How do I parse this so that I can store each value in a seperate variable?

Thanks in advance,
-Russ
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start with you can use the split() method of String to split the string into separate fields based on the comma separator.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to have a grammar before you can parse the String.

Is it simply a set of values separated by commas? In which case try this String method. Make sure to go through a tutorial about regular expressions to check that the comma isn't a meta-character.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I'm not sure what you mean about "a grammar".
> Is it simply a set of values separated by commas? In which case try this String method.
I don't know which "this" method you are referring to.

Any advice on using the split method?

Thanks again,
-Russ
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by 'grammer' he means what EXACTLY determines where one value starts, and another ends. Do you need to pull out the month/day/year in addition? can there ever be a comma INSIDE a single field? etc.

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks for the explanation.

I need to pull the dates as whole dates (not seperating out month or day or year).

There will never be a comma inside a single field.

The format will always be the 4 values in this comma-seperated format:
"IBM",123.65,+0.84,"2/11/2010"

I've been playing with the split() method and it seems to be doing the job.

Thank you very much.
-Russ

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's your grammar: "company" comma number comma number comma "date"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!