0

I know how to remove elements from an array if i know what they are.

The "client" in this array is an string input by the user. I want to remove the first 2 words, but I don't know what they'll be. Its always different.

string[] words = Receipt.Split(' ',',','-');

Thanks.

2 Answers 2

4
string[] words = Receipt.Split(' ',',','-').Skip(2).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

3

If you always want to remove the first two, LINQ probably provides the simplest approach:

string allButFirstTwoWords = words.Skip(2).ToArray(); // Or ToList, or nothing...

Note that you can't "remove" values from the array itself, as an array always has a fixed size after creation. The code above creates a new array with all but the first two words.

Of course, you can do it all in one go:

string[] words = Receipt.Split(' ', ',', '-').Skip(2).ToArray();

Personally I'd usually use a List<string> instead, as it's more flexible:

List<string> words = Receipt.Split(' ', ',', '-').Skip(2).ToList();

If you're just going to iterate over it, you don't need to convert it to an array or a list at all:

IEnumerable<string> words = Receipt.Split(' ', ',', '-').Skip(2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.