2

I'm looking to do something like this

public string[] getArray() {

    string text = "1 ab cd 2 ef gh 3 ij kl 4 mn";
    string[] arr = text.Split(" ").remove(every third element); //remove the 1,2,3,4 etc

    return arr;
}
  • Does this answer your question? [How can I get every nth item from a List?](https://stackoverflow.com/questions/682615/how-can-i-get-every-nth-item-from-a-listt) – Prasad Telkikar Mar 05 '20 at 05:45

1 Answers1

2

You can use Linq to skip every nth element.

public string[] getArray() {

    string text = "1 ab cd 2 ef gh 3 ij kl 4 mn";
    string[] arr = text.Split(" ").Where((x, i) => i % 3 != 0).ToArray();
    return arr;
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • 2
    I guess you made some mistake and mean `text.Split(' ').Where((x, i) => (i) % 3 != 0).ToArray()` – Zephyr Mar 05 '20 at 02:30
  • 2
    I honestly didn't read the question very closely. It's going to be some variation of `Where` and `%3`. – John Wu Mar 05 '20 at 02:31