It's not a secret anymore!
The moose likes Java in General and the fly likes String parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String parsing" Watch "String parsing" New topic

String parsing

Karthik Rallapalli

Joined: Jul 01, 2004
Posts: 13
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.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375

Your welcome.
[ February 17, 2005: Message edited by: Kashif Riaz ]
I agree. Here's the link:
subject: String parsing
It's not a secret anymore!