0

I have a text file with various 16 char strings both appended to one another and on separate lines. I've done this

FileInfo f = new FileInfo("d:\\test.txt");
string FilePath = ("d:\\test.txt");
string FileText = new System.IO.StreamReader(FilePath).ReadToEnd().Replace("\r\n", "");
CharCount = FileText.Length;

To remove all of the new lines and create one massively appended string. I need now to split this massive string into an array. I need to split it up on the consecutive 16th char until the end. Can anyone guide me in the right direction? I've taken a look at various methods in String such as Split and in StreamReader but am confused as to what the best way to go about it would be. I'm sure it's simple but I can't figure it out.

Thank you.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Herblore
  • 1
  • 3

5 Answers5

0

Create a list to hold your tokens. Then get subsequent substrings of length 16 and add them to the list.

List<string> tokens = new List<string>();
for(int i=0; i+16<=FileText.Length; i+=16) {
    tokens.Add(FileText.Substring(i,16));
}

As mentioned in the comments, this ignores the last token if it has less than 16 characters. If you want it anyway you can write:

List<string> tokens = new List<string>();
for(int i=0; i<FileText.Length; i+=16) {
    int len = Math.Min(16, FileText.Length-i));
    tokens.Add(FileText.Substring(i,len);
}
Danvil
  • 22,240
  • 19
  • 65
  • 88
0

Adapting the answer from here:

You could try something like so:

        string longstr = "thisisaverylongstringveryveryveryveryverythisisaverylongstringveryveryveryveryvery";
        IEnumerable<string> splitString = Regex.Split(longstr, "(.{16})").Where(s => s != String.Empty);
        foreach (string str in splitString)
        {
            System.Console.WriteLine(str);
        }

Yields:

thisisaverylongs    
tringveryveryver    
yveryverythisisa    
verylongstringve    
ryveryveryveryve
ry
Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0

One possible solution could look like this (extracted as extension method and made dynamic, in case different token size is needed and no hard-coded dependencies):

public static class ProjectExtensions
{
    public static String[] Chunkify(this String input, int chunkSize = 16)
    {
        // result
        var output = new List<String>();
        // temp helper
        var chunk = String.Empty;
        long counter = 0;
        // tokenize to 16 chars
        input.ToCharArray().ToList().ForEach(ch => 
        {
            counter++;
            chunk += ch;
            if ((counter % chunkSize) == 0)
            {
                output.Add(chunk);
                chunk = String.Empty;
            }
        });
        // add the rest
        output.Add(chunk);

        return output.ToArray();
    }
}

The standard usage (16 chars) looks like this:

// 3 inputs x 16 characters and 1 x 10 characters
var input = @"1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890";
foreach (var chunk in input.Chunkify())
{
    Console.WriteLine(chunk);
}

The output is:

1234567890ABCDEF
1234567890ABCDEF
1234567890ABCDEF
1234567890

Usage with different token size:

foreach (var chunk in input.Chunkify(13))
{
    Console.WriteLine(chunk);
}

and the corresponding output:

1234567890ABC
DEF1234567890
ABCDEF1234567
890ABCDEF1234
567890

It is not a fancy solution (and could propably be optimised for speed), but it works and it is easy to understand and implement.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
-1

Please try this method. I haven't tried it , but used it once.

    int CharCount = FileText.Length;
    int arrayhold = (CharCount/16)+2;
    int count=0;
    string[] array = new string[arrayhold];
    for(int i=0; i<FileText.Length; i+=16)
       {
          int currentleft = FileText.Length-(16*count);
          if(currentleft>16)
            {
                  array[count]=FileText.Substring(i,16);
            }
          if(currentleft<16)
            {
                  array[count]=FileText.Substring(i,currentleft);
            }

              count++;
        }

This is the new code and provide a TRUE leftovers handling. Tested in ideone

Hope it works

Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23
-1

Try this one:

var testArray = "sdfsdfjshdfalkjsdfhalsdkfjhalsdkfjhasldkjfhasldkfjhasdflkjhasdlfkjhasdlfkjhasdlfkjhasldfkjhalsjfdkhklahjsf";
var i = 0;
var query = from s in testArray
            let num = i++
            group s by num / 16 into g
            select new {Value = new string(g.ToArray())};

var result = query.Select(x => x.Value).ToList();

result is List containing all the 16 char strings.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321