How to split a string on the nth occurrence?
c#
Solution
There is nothing built in.
You can use the existing `Split`, use `Take` and `Skip` with `string.Join` to rebuild the parts that you originally had.
string[] items = input.Split(new char[] {'\t'},
StringSplitOptions.RemoveEmptyEntries);
string firstPart = string.Join("\t", items.Take(nthOccurrence));
string secondPart = string.Join("\t", items.Skip(nthOccurrence))
string[] everythingSplitAfterNthOccurence = items.Skip(nthOccurrence).ToArray();
An alternative is to iterate over all the characters in the string, find the index of the nth occurrence and substring before and after it (or find the next index after the nth, substring on that etc... etc... etc...).
Problem
What I want to do is to split on the nth occurrence of a string (in this case it's "\t"). This is the code I'm currently using and it splits on every occurrence of "\t". ``` string[] items = input.Split(new char[] {'\t'}, StringSplitOptions.RemoveEmptyEntries); ``` If input = "one\ttwo\tthree\tfour", my code returns the array of: - one - two - three - four But let's say I want to split it on every "\t" after the second "\t". So, it should return: - one two - three - four