You may take the following approach-
1. Find the index of 3rd comma from the supplied string variable
public int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
Reference:
Find Nth occurrence of a character in a string
2.Remove the comma & add carriage return character after that index like
string yourString="sar john,texas,dellas,ny,111111,";
string finalString;
finalString=yourString;
int indexOfThirdIndex=0;
int counter=3;
while(indexOfThirdIndex!=(-1))
{
int indexOfThirdIndex=GetNthIndex(finalString,',',counter);
if(indexOfThirdIndex!=(-1))
{
finalString=finalString.Substring(0, indexOfThirdIndex).TrimEnd(',')+"\r\n"+inalString.Substring(indexOfThirdIndex);
}
counter+=3;
}
Hope, it helps :)
N:B: I haven't tested all these in my system but the approach should work. Please let me know if it doesn't.