Say I have a string var s = "123456789"
where
foreach(var c in DivideStr(s, 3)){
Console.WriteLine(c);
}
would print out 123 , 456, 789
This is a fairly easy problem to accomplish with loop and if statement. But I want to accomplish with Take and Skip function in C# in the follow fashion
IEnumerable DivideStr(String s, Int n)
{
var a = s;
while(!a.IsEmpty())
{
yield return a.Take(n)
a = a.Drop(3) // or a.Skip(n)
}
}
This way, if I have var s = "12345678"
The print out would be 123, 456, and 78
The problem is the above code won’t compile. What am I missing?
Try something like this
Or this