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]
<
var s = "TVBMCVTVFGTVTB";
var pairs = Enumerable.Range(0, s.Length / 2)
.Select(i => String.Concat(s.Skip(i * 2).Take(2)));
This will work if you know that s
always is of even length, or you don't accept, or don't care about, ending with singletons for strings with odd length.
If you want to include singleton remainders, for odd length strings, you can simply use ceiling:
var s = "TVBMCVTVFGTVTB";
var pairs = Enumerable.Range(0, (int)Math.Ceiling(s.Length / 2D))
.Select(i => String.Concat(s.Skip(i * 2).Take(2)));