• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to split String array which has comma seperated values

 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21418
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic