C# - split String into smaller Strings by length variable

Posted by tyndall on Stack Overflow See other posts from Stack Overflow or by tyndall
Published on 2010-06-09T18:29:25Z Indexed on 2010/06/09 18:32 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

I'd like to break apart a String by a certain length variable.
It needs to bounds check so as not explode when the last section of string is not as long as or longer than the length. Looking for the most succinct (yet understandable) version.

Example:

string x = "AAABBBCC";
string[] arr = x.SplitByLength(3);
// arr[0] -> "AAA";
// arr[1] -> "BBB";
// arr[2] -> "CC"

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET