Suppose I had a string:
string str = "1111222233334444";
How can I break this string into chunks of some size?
e.g., breaking this into sizes of 4 would return strings:
"1111"
"2222"
"3333"
"4444"
Suppose I had a string:
string str = "1111222233334444";
How can I break this string into chunks of some size?
e.g., breaking this into sizes of 4 would return strings:
"1111"
"2222"
"3333"
"4444"
This can be done in this way too
If necessary to split by few different length: For example you have date and time in specify format string
strangeStr = "07092016090532";
07092016090532 (Date:07.09.2016 Time: 09:05:32)using:
How's this for a one-liner?
With this regex it doesn't matter if the last chunk is less than four characters, because it only ever looks at the characters behind it. I'm sure this isn't the most efficient solution, but I just had to toss it out there. :D
Six years later o_O
Just because
or
AFAIK all edge cases are handled.
I recently had to write something that accomplishes this at work, so I thought I would post my solution to this problem. As an added bonus, the functionality of this solution provides a way to split the string in the opposite direction and it does correctly handle unicode characters as previously mentioned by Marvin Pinto above. So, here it is:
Also, here is an image link to the results of running this code: http://i.imgur.com/16Iih.png