Maybe a basic question but let us say I have a string that is 2000 characters long, I need to split this string into max 512 character chunks each.
Is there a nice way,
Though this question meanwhile has an accepted answer, here's a short version with the help of regular expressions. Purists may not like it (understandably) but when you need a quick solution and you are handy with regexes, this can be it. Performance is rather good, surprisingly:
string [] split = Regex.Split(yourString, @"(?<=\G.{512})");
What it does? Negative look-backward and remembering the last position with \G
. It will also catch the last bit, even if it isn't dividable by 512.