Does c# has a similiar method to splice
from JavaScript?
I only know RemoveRange
, and this does not return the removed elements:
List<string> t = new List<string>();
t.RemoveRange(..., ...);
(I want to avoid writing my own collection).
The splice() method is a built-in method for JavaScript Array objects. It lets you change the content of your array by removing or replacing existing elements with new ones. This method modifies the original array and returns the removed elements as a new array.
The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
The slice () method returns the extracted part in a new string. The slice () method does not change the original string. The start and end parameters specifies the part of the string to extract. The first position is 0, the second is 1, ... A negative number selects from the end of the string.
let months = ["January", "February", "Monday", "Tuesday"]; let days = months.splice (2); console.log (days); // ["Monday", "Tuesday"] The splice () method needs at least one parameter, which is the start index where the splice operation starts.
There is no exact equivelant but you can write one:
public static List<T> Splice<T>(this List<T> source,int index,int count)
{
var items = source.GetRange(index, count);
source.RemoveRange(index,count);
return items;
}
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