Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String parsing

 
Karthik Rallapalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String a=";345;34567;43556;"

I need to parse it in to a string array with {"345","34567","43556"}

I tried using the split method but i am not getting the desired results.
Please help me.

Karthik
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us what you did with split (some code) and what you got.

I like to use delimited strings like this one, with the delimiter first. When you make the string you can choose any delimiter you like. When you parse the string you take the first character for the delimiter. So much cooler than trying to hard code a delimiter character. You can nest strings and all kinds of fun things.

That said, ask yourself if the leading delimiter is causing your problem. You might have to take it off to make split do what you like.

Pulling parts out of strings is so much fun that Java gives you many ways to do it. Look at StringTokenizer, String.split (you're already there), Pattern & Matcher, and Scanner for some options. And there is always indexOf() and substring() if you really want to do it yourself.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Your welcome.
[ February 17, 2005: Message edited by: Kashif Riaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic