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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
why not use split:

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

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.
Mick BarryJava DeveloperCommented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jollykhAuthor Commented:
Thanks CEHJ!!!  That is exactly what I was looking for.
CEHJCommented:
:-)
CEHJCommented:
>>with NO explanations in such question

v. difficult to explain regex
VenabiliCommented:
Not that difficult to give a URL for a tutorial for this :)And links on whta Matcher, Pattern and so on are :)
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.
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 ...
Mick BarryJava DeveloperCommented:
> 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 :)
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.