Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
2.20/5 (3 votes)
See more:
I have this string:

string="101,102,103,105,201,250,2564,245564,212,2415,2102,5645,656";


I want to split it into groups of 5.

Output should be:

C#
array[0]=101,102,103,105,201
     [1]=250,2564,245564,212,2415
     [2]=2102,5645,656
Posted
Updated 25-May-14 20:32pm
v2
Comments
Sergey Alexandrovich Kryukov 26-May-14 2:34am    
You did not explain by what criteria do you need to split them. An example is never a definition. But a correct definition is more than half of the solution. And what's wrong with just reading standard MSDN documentation.

For the author of 78 answers, this question does not really look appropriate... :-)

—SA

Please see my comment to the question: you did not really define what you want to achieve.

The problem is way too simple. You need just two things 1) correct definition of your requirements, 2) this: http://msdn.microsoft.com/en-us/library/system.string.split.aspx[^].

Probably you mean that you want to split the items by groups of 5 elements delimited by ',' and put the remaining elements (less or equal than 5) in a last group. If so, you also need to know this:
http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx[^],
http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx[^]. :-)

—SA
 
Share this answer
 
v3
Comments
Pranav-BiTwiser 26-May-14 2:48am    
do u know how to delete the question?? which has been asked
King Fisher 26-May-14 3:04am    
why do you want to delete ?
Ajith K Gatty 26-May-14 3:27am    
Hi.....You dont delete this question. Just keep it. Mistakes do happen just learn from it. Mr. Sergey is a nice guy. He corrected me once. Follow his advice.
Pranav-BiTwiser 26-May-14 3:27am    
coz it is not appropriate....and has been posted by my colleague, not me
From Brain-To-Paper:
C#
string pattern = @"(?:^|,)(\d+(?:,\d+){1,4})";
var array = Regex.Matches(input, pattern)
                 .Cast<Match>()
                 .Select(m=>m.Groups[1].Value)
                 .ToArray();

This should work assuming the string is well formed.

[EDIT] I've tested the above code and it works (so, no any more "from-brain-to-paper"). The code below is crap, though - sorry for the wrong hint :-(
Alternatively
C#
IEnumerable<string> GetChunks(string input)
{
    var elements = input.Split(new char[]{','});
    foreach(var chunk in elements.Take(5))
    {
        yield return string.Join(",", chunk);  
    }
}
var array = GetChunks(input).ToArray();


[/EDIT]

Cheers
Andi
 
Share this answer
 
v3
C#
string str ="101,102,103,105,201,250,2564,245564,212,2415,2102,5645,656,444,555";
            string[] arl = str.Split(',');
            if (arl.Length > 5)
            {
                string[] strResult;
                int totCount = arl.Length / 5;
                int totReminder = arl.Length % 5;

                if (totReminder > 0)
                {
                    strResult = new string[totCount + 1];
                }
                else
                {
                    strResult = new string[totCount];
                }

                for (int i = 0; i < totCount; i++)
                {
                    strResult[i] = arl[i].ToString() + "," + arl[i + 1].ToString() + "," + arl[i + 2].ToString() + "," + arl[i + 3].ToString() + "," + arl[i + 4].ToString();
                }
                if (totReminder > 0)
                {
                    for (int i = 5 * totCount; i < arl.Count(); i++)
                    {
                        strResult[strResult.Length - 1] = strResult[strResult.Length - 1] + arl[i];
                        if (i < (arl.Count() - 1))
                        {
                            strResult[strResult.Length - 1] = strResult[strResult.Length - 1] + ",";
                        }
                    }
                }
            }
            else
            {
            //do the needFull
            }
 
Share this answer
 
Comments
Aravind Garre 3-Jun-14 8:38am    
Declare Arraylist then split by comma then u will get in arraylist

arraylist arraryl=string.split(",");

Hope this will help you...
Ajith K Gatty 5-Jun-14 1:20am    
Solution looks nice.
Now that my article: Considerations on Efficient use of LINQ Extension Methods[^] is complete I can suggest:
C#
string str ="101,102,103,105,201,250,2564,245564,212,2415,2102,5645,656,444,555";
string[] groups = str.Split(',').Chunkify(5).Select(chk => string.Join(",", chk)).ToArray();

The Chunkify1 implementation (as well as Chunkify) from the article will work just fine in this case.
(This depends on .NET 4 which added the string.Join() overload taking IEnumerable<string> as the second argument.)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900