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???
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.
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.
The indices for a 100 element array range from 0 to 99.
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();
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