Is there a way to break a string into pairs without looking at indexes? e.g. TVBMCVTVFGTVTB would be broken into a list of strings as such:
[TV,BM,CV,TV,FG,TV,TB]
<
Oh come on, just use indexes like this:
public static class StringExtensions {
public static IEnumerable TakeEvery(this string s, int count) {
int index = 0;
while(index < s.Length) {
if(s.Length - index >= count) {
yield return s.Substring(index, count);
}
else {
yield return s.Substring(index, s.Length - index);
}
index += count;
}
}
}
I have added no guard clauses.
Usage:
var items = "TVBMCVTVFGTVTB".TakeEvery(2);
foreach(var item in items) {
Console.WriteLine(item);
}