# Split String based on a Character Occurrence

Hi,

I want to split a string based on the nth occurrence of comma(,).

StringValue = a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

I want to split the string after the 20th occurrence of commas and save them into an array.

ArrayValue[0] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t}
ArrayValue[1] = {u,v,w,x,y,z}

I can able to split each string into single array elements. Can anyone help me on this?

Thanks,

How to find the nth occurrence of a character in a string?

Hi @john.mari ,

You could Try the below Expression :

Split(StringValue,",").Take(20).ToArray

Split(StringValue,",").Skip(20).ToArray

We have two arrays created and you could append them these two arrays to another Two Dimensional Array or a List.

I TRIED THE ABOVE METHOD ITS NOT SUITABLE FOR MY REQUIREMENT. CAN YOU GUIDE ME TO FIND NTH OCCURRENCE OF COMMA IN A STRING

@john.mari , I am not sure How would this be helpful. Could you explain your Requirement in a bit more detail ?

We could find the Nth occurrence, but Do you need to find it’s position like index ?

I found the solution. Thanks for your help.

We would appreciate if you could post the Solution that you have found and Close this Topic by Marking it as the Solution.

This would help others who are in search for the same kind of solution.

A quick dirty approach

With an index calculation like:

Here we can get n number of values like a,b, and c. Like if we have 147 values we have to split it into 20 values as one value and the other as 20 so on.

with the last shown options you can flexible calculate the split mark index
When inserting from highest to lowest index order the index will not be confused
once the split marks are inserted you can split with the split method

In case you need more help then just another example where more splits are to calculate

I figured out and solved the issue. Thanks for your guidance

Perfect, so as @supermanPunch has mentioned may we ask you to close the topic. Thanks