SoDamnMetal
SoDamnMetal

Reputation: 77

How can i split a string and remove every nth element?

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

Upvotes: 2

Views: 421

Answers (1)

John Wu
John Wu

Reputation: 52280

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

Upvotes: 2

Related Questions