Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to divide a text file in user define strings ..... and if the user ask to divide the text file in 10 parts then every divided string should be of same size (bytes) ......i have

used the substring method in c# but it doesn't fulfill my needs and i am using c#

give me any suggestion or any possible solution .........................................

and i have to do this using c# only

................................................................thanks
Posted
Comments
BillWoodruff 29-Jan-14 3:10am    
And, it doesn't matter if you chop words into two parts ?

What happens if the length of your data string is not evenly divisible by the chunk size: in that case unless you create a string to hold the "remainder" padded with some character so its size is equal to the chunk size: you will not preserve all the characters in the data string.
Adi5555 29-Jan-14 4:02am    
the difference of 1 byte or 2 byte in the size is acceptable but not more than that
BillWoodruff 29-Jan-14 4:05am    
Logically, the maximum "remainder" you could have would be chunkSize - 1 in length.

use this method:

C#
static IEnumerable<string> SplitText(string str, int chunkSize)
{
  if (str.Length < chunkSize)
   {
     return new string[]{str};
   }
   return Enumerable.Range(0, str.Length / chunkSize)
         .Select(i =>
                   ((i * chunkSize) + chunkSize) <= str.Length ?
                   str.Substring(i * chunkSize, chunkSize):
                   str.Substring((i*chunkSize), str.Length-(i*chunkSize))
               );
}

use it like:
C#
string mytext = "Quick brown fox jumps over the lazy dog!123456789";
string[] chunks = SplitText(mytext, 4).ToArray();


HTH !
 
Share this answer
 
v3
Comments
BillWoodruff 29-Jan-14 3:39am    
+4
Nice use of Linq ! The problem with this code is that if the string being "chunked"'s length is not evenly divisible by the chunkSize, characters will be left out.

However, the OP doesn't say what to do in this case.

If you can modify your code to handle (string length) % chunkSize != 0 ... you get a 5.
Sunny_Kumar_ 29-Jan-14 4:25am    
thanks for pointing out... modified my answer to handle last chunk of text. Thanks!
BillWoodruff 29-Jan-14 5:21am    
+5 :)
Sunny_Kumar_ 29-Jan-14 5:23am    
thanks :)
Here's an outline of a very plain one step-at-a-time solution:
C#
List<string> chunks = new List<string>();

string mytext = "Quick brown fox jumps over the lazy dog yet we!";

int sLen = mytext.Length;

int chunkSize = 4;

int remainder = sLen % chunkSize;

string pad = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

for (int i = 0; i < sLen; i += chunkSize)
{
    if(i + chunkSize <= sLen)
    {
        chunks.Add(mytext.Substring(i, chunkSize));
    }
    else
    {
        chunks.Add(mytext.Substring(i, remainder) + pad.Substring(0, chunkSize - remainder));
    }
}
In this solution the case that the length of the data string is not evenly divisible by the chunk size is handled by padding the "remainder" characters in the last element in the List with tilde characters (~).
 
Share this answer
 
Comments
Sunny_Kumar_ 29-Jan-14 5:24am    
my +5 for simple and no LINQ usage code! It keeps the chunk size even.
Try this..

C#
int size = 5;

double each = (double) mytext.Length / (double)size;
List<string> lstOut = new List<string> ();
for (int i = 0; i < size; i++)
    lstOut.Add(mytext.Substring(i * (int)each ,(int)each));
 
Share this answer
 
Comments
Adi5555 29-Jan-14 3:57am    
but u have hard coded the size ...... in my case the size dependce upon the user upload
Karthik_Mahalingam 29-Jan-14 4:11am    
yes you can pass any size values,
for testing and demo i have used 5..
BillWoodruff 29-Jan-14 4:04am    
All the conversions to double and int are not necessary for a solution here.

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