Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript splice in c#

Tags:

arrays

c#

.net

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).

like image 682
BendEg Avatar asked Mar 03 '15 13:03

BendEg


People also ask

What is the use of splice in JavaScript?

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.

How to change the contents of an array using splice?

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

What is the use of slice () method in JavaScript?

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.

How do you splice a month in Python?

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.


1 Answers

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;
}
like image 66
Selman Genç Avatar answered Sep 22 '22 13:09

Selman Genç