Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove first element from array

Tags:

c#

PHP developer here working with c#. I'm using a technique to remove a block of text from a large string by exploding the string into an array and then shifting the first element out of the array and turning what remains back into a string.

With PHP (an awesome & easy language) it was just

$array = explode('somestring',$string);
array_shift($array);
$newstring = implode(' ', $array);

and I'm done.

I get so mad at c# for not allowing me to create dynamic arrays and for not offering me default functions that can do the same thing as PHP regarding arrays. Instead of dynamic arrays I have to create lists and predefine key structures etc. But I'm new and I'm sure there are still equally graceful ways to do the same with c#.

Will someone show me a clean way to accomplish this goal with c#?

Rephrase of question: How can I remove the first element from an array using c# code.

Here is how far I've gotten, but RemoveAt throws a error while debugging so I don't believe it works:

//scoop-out feed header information
if (entry_start != "")
{
    string[] parts = Regex.Split(this_string, @entry_start);
    parts.RemoveAt(0);
    this_string = String.Join(" ", parts);
}
like image 536
atwellpub Avatar asked Feb 09 '12 22:02

atwellpub


People also ask

How do I remove the first element?

The shift() method removes the first item of an array. The shift() method changes the original array.

How do I remove the first element from an array in Java?

We can use the remove() method of ArrayList container in Java to remove the first element. ArrayList provides two overloaded remove() method: remove(int index) : Accept index of the object to be removed. We can pass the first element's index to the remove() method to delete the first element.

How do you remove an element from an array array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do you remove the first element from an array without mutation?

Remove the first element of an array with shiftThe shift method can be used on an array to remove the first element of an array. If you have an array named arr it can be used in this way: arr. shift() . The shift method removes the first item of the array.


1 Answers

I get so mad at c# for not allowing me to create dynamic arrays

You may take a look at the List<T> class. Its RemoveAt might be worth checking.

But for your particular scenario you could simply use LINQ and the Skip extension method (don't forget to add using System.Linq; to your file in order to bring it into scope):

if (entry_start != "")
{
    string[] parts = Regex.Split(this_string, @entry_start).Skip(1).ToArray();
    this_string = String.Join(" ", parts);
}
like image 138
Darin Dimitrov Avatar answered Oct 06 '22 00:10

Darin Dimitrov