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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shubham KumarPosted Feb 22, 2016, 4:02 AM
Sujeet SumanPosted Feb 22, 2016, 4:16 AM
ali tuncerPosted Feb 22, 2016, 4:06 AM
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)+ ",");
}