File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to split String array which has comma seperated values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to split String array which has comma seperated values" Watch "How to split String array which has comma seperated values" New topic
Author

How to split String array which has comma seperated values

shabarish vai
Ranch Hand

Joined: May 26, 2008
Posts: 79
Hello all

i have an String which has as an comma separated value example:"1,2,3,4,5,6,7,8,...,3500"
the comma separated value has more than 2000 value how can i split by comma from 1 to 999 and from 999 to 1998.

can you provide the solution for the above question ASAP


thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can you provide the solution for the above question ASAP

You're misunderstanding the purpose of the ranch. This is not a code mill.

I'm moving your post to a more appropriate forum.


[My Blog]
All roads lead to JavaRanch
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
It seems you can't use a generic solution like String.split or something similar, so the solution to your problem is parse manually the String.
You can do that by getting each char in a loop, put chars in a current string, when you encounter the "," char you interpret the current string as number, and go over and over again.


http://javasign.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Aurelian Tutuianu wrote:It seems you can't use a generic solution like String.split or something similar

Well it is possible, as an intermediate solution. Split the string, then iterate over the array putting array elements together in larger strings.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
Right, I was thinking in the first time on that. By "can't use" I mean "is not appropriate to use". I was thinking that if he don't want to have many values parsed, than perhaps he does not want all value parsed.
Technically I totally agree with your solution.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

You could use java.util.Scanner. This will iterate through each number as it finds them, as opposed to String.split(), which will hold all the numbers in memory in one giant array.


SCJP 6 || SCWCD 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40


Another option... instead of using the regex split() method, you could use the regex find() method. With find(), you can defined the token to include, up to 999 values separated by commas -- and hence, no need to split and remerge 999 items.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to split String array which has comma seperated values