I want to split a string by comma while choosing the chunk size.
That is
String="abcdefghij"
chunksize=3
output=abc,def,ghi,j
I have to do it in c# (asp.net ).Please help me!!!
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Shubham KumarRank #621 · 3.8K reputationPosted Feb 21, 2016, 11:02 PM EST
ali tuncerRank #1,498 · 1.1K reputationPosted Feb 21, 2016, 11:06 PM EST
Many ways to do this, here is one:
string str = "abcdefghij";
int chunkSize = 3;
for (int i = 0; i < str.Length; i += chunkSize)
{
if (i + chunkSize > str.Length)
{
chunkSize = str.Length - i;
}
Console.Write(str.Substring(i, chunkSize)+ ",");
}
Sujeet SumanRank #790 · 2.9K reputationPosted Feb 21, 2016, 11:16 PM EST