Find the last index of a character, split it, and add it to an array

Hello Experts,

I want to split a string into an array in chunks of 10, but here's the catch:  I need to find the last index of a comma (,) in the chuck and split it into an array, even if it's less than 10.

For example:

String s = "1234567890,555,666666,9999,44,222222222";

// My first array should contain the value "1234567890". (10)
// My second array should contain the value "555,666666" (10)
// My third array should contain "9999,44" (7)
// My fourth array should contain "222222222" (9)

Here is the code I have so far but it's splitting the string into chunks of 10 regardless of the last array.

String chopSize = 10;
String toSplit = "1234567890,555,666666,9999,44,222222222";
List filler = new ArrayList();  
 for (int i=0, from=0, to=0, length = toSplit.length(); chopSize*i < length; i++) {
            from = chopSize*i;
            to = Math.min(from + chopSize, length);
            final String substring = toSplit.substring(from, to);
            filler.add(i,substring);
        }

Can someone tell me where I'm going wrong or point me in the right direction?  Thanks!!
jollykhAsked:
Who is Participating?
 
CEHJCommented:
           String s = "1234567890,555,666666,9999,44,222222222";
            Pattern p = Pattern.compile("([^,]{10}|[^,]+,[^,]+|[^,]+)");
            Matcher m = p.matcher(s);
            while (m.find()) {
                  System.out.println(m.group(1));
            }


prints

1234567890
555,666666
9999,44
222222222


but give it thorough testing as it may violate your rules in certain cases
0
 
objectsCommented:
why not use split:

Strin[] split = s.split(",");

0
 
jollykhAuthor Commented:
Because that splits the string at each "," interval so my results will be:

1234567890
555
666666
9999
44
222222222

... and that is not what I want.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
objectsCommented:
then you loop thru, concatenating the tokens such that the length is <=10.
for each token
    if current lenght + next token length <= 10
        append token to current
    else
        add current to result
        clear current
    end if
loop
0
 
jollykhAuthor Commented:
Thanks CEHJ!!!  That is exactly what I was looking for.
0
 
CEHJCommented:
:-)
0
 
CEHJCommented:
>>with NO explanations in such question

v. difficult to explain regex
0
 
VenabiliCommented:
Not that difficult to give a URL for a tutorial for this :)And links on whta Matcher, Pattern and so on are :)
0
 
jollykhAuthor Commented:
CEHJ is right.....the Matcher and Pattern are fairly difficult to explain.

One thing I noticed, however, was that the code doesn't work like it should.  If you replace the string "1234567890,555,666666,9999,44,222222222" with something different like : "123,456,7777,8833,345345,345,3,4,5,6,6,6,7,8,345" it doesn't work.  The prgram doesn't take the first 10 characters and finds the last instance of a "," and prints it out.  In other words, the program is not very dynamic and it needs to be reworked.  I will try to figure out a more robust solution and show the solution once I am finished.
0
 
CEHJCommented:
Well it's difficult to understand your rules fully. The first three tokens returned are

123,456
7777,8833
345345,345

which is actually what i though you wanted ...
0
 
objectsCommented:
> One thing I noticed, however, was that the code doesn't work like it should.

You can always reopen the q and I'll help you with implementing what I suggested :)
0
 
jollykhAuthor Commented:
Hi CEHJ,
Yes, you are correct.  That was what I wanted, but that was just an example of the requirement.  The example string I gave you, "1234567890,555,666666,9999,44,222222222", was just that: an example but the string can be changed to reflect other values.  
I plan on taking a closer look at the program this weekend as I really don't understand the Pattern and Matcher objects.  If I can't figure it out then I may have to reopen the question.
thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.