Here's a solution that uses LINQ to take a set of characters based on a list of specified "pages."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//...
public static void TestStringSplit()
{
var s = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.";
var chars = s.ToCharArray().AsEnumerable();
var pages = new List<int>() { 8, 4, 5, 3, 50, 50 };
var output = new List<string>();
for (int i = 0; i < pages.Count; i++)
{
var page = pages[i];
output.Add(String.Join("",
chars.Skip(pages.Where((p, j) => j < i).Sum(p => p)).Take(page)
));
}
output.ForEach(o => Console.WriteLine(o));
}
returns:
12345678
9012
34567
890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890.
In the above example,
- The
pages
variable represents the the size of each substring that you want to take from the main string.
- The
Skip
LINQ expression is skipping all of the characters that have already been "paged."
- The
Take
LINQ expression is returning a group of characters that match the size you are expecting.
The advantage of using an enumerable of char
s is that you don't get index out of range exceptions in the Skip
call like you normally would with a Substring
call.
How you fill the pages
variable with data and how you get the values in output
into your database is dependent on your implementation, but this should give you a good start.