C Sharp - Useful Code [GetChunks]

Deepak
Posted by Deepak under C# category on | Views : 3999
This method gets the chunks out of a string...

 public string[ ] GetChunks( string entireString , int chunkSize )
{
int entireStringLength = entireString.Length;
int remainder = entireStringLength % chunkSize;
int quotient = entireStringLength / chunkSize;
int totalChunks = ( remainder > 0 ) ? quotient + 1 : quotient;
string[ ] result = new string[ totalChunks ];
int fromPosition = 0;
if ( chunkSize > entireStringLength )
{
chunkSize = entireStringLength;
}
int chunkLength = chunkSize;
for ( int currentChunkIndex = 0 ; currentChunkIndex < totalChunks ; currentChunkIndex++ )
{
result[ currentChunkIndex ] = entireString.Substring( fromPosition , chunkLength );
fromPosition += chunkLength;
if ( fromPosition + chunkLength > entireStringLength )
{
chunkLength = entireStringLength - fromPosition;
}
}
return result;
}


Comments are appreciated...

Comments or Responses

Posted by: Hardcoder on: 2/28/2009 Level:Starter | Status: [Member]
Please provide some more details about your code, like what this code will do and where others can use it.

What I understand this code will take a large string and a chunk size , and then it will break the large strings into this chunk size store these small strings into an array (of string) and returns.

Thanks, Irfan

Login to post response