Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ranges from an array in C#

How to remove ranges from an array in C# Like with ArrayList?

ArrayList myAL = new  ArrayList();

myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );   
myAL.RemoveRange( 4, 3 );

How can i achieve the same with string array object???

like image 452
RollerCosta Avatar asked Dec 22 '11 12:12

RollerCosta


People also ask

How do you delete a range of an element from an array?

You can remove elements from the end of an array using pop, from the beginning using shift, or from the middle using splice. The JavaScript Array filter method to create a new array with desired items, a more advanced way to remove unwanted elements.

Can you remove elements from an array in C?

In C programming, an array is derived data that stores primitive data type values like int, char, float, etc. To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array.

What is the range of array in C?

The indices for a 100 element array range from 0 to 99.


1 Answers

Generic Lists expose a RemoveRange() method. You can convert your array to a list, then remove the range, then convert back to an array:

var myList = myArray.ToList();
myList.RemoveRange(index, count);
myArray = myList.ToArray();

To remove only one item at a specific index you can use RemoveAt():

var myList = myArray.ToList();
myList.RemoveAt(index);
myArray = myList.ToArray();
like image 87
Dennis Traub Avatar answered Sep 19 '22 07:09

Dennis Traub