In C#, calling the .Split
method will split a string into an array of strings based on some character or string.
Is there an equivalent method for lists or arrays?
For example:
var foo = new List<int>() { 1, 2, 3, 0, 4, 5, 0, 6 };
var output = Split(foo, 0);
// produces { { 1, 2, 3 }, { 4, 5 }, { 6 } }
This is what I have so far -- is there a cleaner or more eloquent way of accomplishing the same task?
IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> list, T divider)
{
var output = new List<List<T>>();
var temp = new List<T>();
foreach ( var item in list )
{
if (item.Equals(divider))
{
output.Add(temp);
temp = new List<T>();
}
else
{
temp.Add(item);
}
}
output.Add(temp);
return output;
}
Edit:
It just occurred to me that my version will split the list only by a single element, whereas string.Split
can split using either a single character, or an arbitrary string.
Just for the sake of completeness, what would be the best way to implement that?
Usually, we use a comma to separate three items or more in a list. However, if one or more of these items contain commas, then you should use a semicolon, instead of a comma, to separate the items and avoid potential confusion.
1) Store the mid and last pointers of the circular linked list using tortoise and hare algorithm. 2) Make the second half circular. 3) Make the first half circular. 4) Set head (or start) pointers of the two linked lists.
There is no such thing as a list in C.
No built-in equivalent, but a lazy-evaluated one would be
IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> list, T divider)
{
var temp = new List<T>();
foreach (var item in list)
{
if (!item.Equals(divider))
{
temp.Add(item);
}
else
{
yield return temp;
temp = new List<T>();
}
}
if(temp.Count>0) yield return temp;
}
No, there is no special existing method in the framework to split sequence.
You code is reasonable.
Routes to improve/change:
yield return
instead of adding to output
to gain some lazy evaluation. Aggregate
if you want to show off single statement code...If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With