atwellpub
atwellpub

Reputation: 6010

remove first element from array

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);
}

Upvotes: 18

Views: 43575

Answers (7)

vi3x
vi3x

Reputation: 290

As stated above, you can use LINQ. Skip(int) will return an IEnumerable<string> that you can then cast back as array.

string[] myArray = new string[]{"this", "is", "an", "array"};
myArray = myArray.Skip(1).toArray();

Upvotes: 2

JYelton
JYelton

Reputation: 36512

You might be more comfortable with generic lists than arrays, which work more like PHP arrays.

List<T>

But if your goal is "to remove a block of text from a large string" then the easier way would be:

string Example = "somestring";
string BlockRemoved = Example.Substring(1);
// BlockRemoved = "omestring"

Edit

I misunderstood the question, thinking you were just removing the first element from the array where the array consisted of the characters that make up the string.

To split a string by a delimiter, look at the String.Split method instead. Some good examples are given here.

Upvotes: 0

John Koerner
John Koerner

Reputation: 38079

Here is the corresponding C# code:

string input = "a,b,c,d,e";
string[] splitvals = input.Split(',');
string output = String.Join(",", splitvals, 1, splitvals.Length-1);
MessageBox.Show(output);

Upvotes: 7

zeal
zeal

Reputation: 740

string split = ",";
string str = "asd1,asd2,asd3,asd4,asd5";
string[] ary = str.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
string newstr = string.Join(split, ary, 1, ary.Count() - 1);

splits at ",". removes the first record. then combines back with ","

Upvotes: 2

Roman Royter
Roman Royter

Reputation: 1665

C# is not designed to be quick and dirty, nor it particularly specializes in text manipulation. Furthermore, the technique you use for removing some portion of a string from a beginning is crazy imho.

Why don't you just use String.Substring(int start, int length) coupled with String.IndexOf("your delimiter")?

Upvotes: 11

Chris Shain
Chris Shain

Reputation: 51319

You can use LINQ for this:

if (entry_start != "")
    this_string = String.Join(" ", Regex.Split(this_string, @entry_start).Skip(1).ToArray());

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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);
}

Upvotes: 39

Related Questions