15,922,007 members
See more:
Dear All,

I want to spit string at specific occurrence of ','
My string is- 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30

i want to separate this string in 3 parts i.e. after 10th occurrence of ',', it should return a string

so my first string will be - 1,2,3,4,5,6,7,8,9,10
and next will be - 11 to 20 and so on

but if i split string at ','. it only separate "1" from string.
Any suggestions?
Posted

[no name] 14-Aug-14 2:26am
I think you have to use Substring
[no name] 14-Aug-14 2:30am
ok. but if i use substring then i have to mention startIndex.
After first 10 values, how can i get startIndex of ','
Sergey Alexandrovich Kryukov 14-Aug-14 2:42am
Could you do one simple thing: sit down and design the algorithm? It looks like you know everything required to solve the problem. The problem is very simple. If you don't teach yourself to solve such simple problem all by yourself, you will experience increasingly unsolvable problems forever...
—SA
[no name] 14-Aug-14 2:44am
thats quite easy you will get the length of the first 10 values no...So add 1 with that value.That is the startIndex of Next

## Solution 7

Yet another approach. As less memory usage as possible, no string reallocation, no regexp. Could be improved? :)

C#
```public IEnumerable<string> SplitBy(string input, char separator, int n)
{
int lastindex = 0;
int curr = 0;

while(curr < input.Length)
{
int count = 0;
while(curr < input.Length && count <n)
{
if(input[curr++] == separator) count++;
}
yield return input.Substring(lastindex,curr-lastindex-(curr < input.Length ? 1 : 0));
lastindex = curr;
}
}

void Main()
{
var inputString = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";

foreach(var s in SplitBy(inputString, ',', 10)) Console.WriteLine(s);
}```

Matt T Heffron 27-Jan-15 11:47am
+5
Zoltán Zörgő 27-Jan-15 12:59pm
Thank you!

## Solution 1

This is what you can try.

1) Split the string on the , using `String.Split`[^].
2) Combine the array based on the occurrence count for e.g. 1 to 10 or 11 to 20 and so on.

[no name] 14-Aug-14 2:29am
Means manually i have to separate?
I have tried array. i get complete array of string i.e. from 1 to 30.
I was about to write the same thing, to convert this to Array and then call the 0 - 10 index and so on. :) I am going to upvote this answer!
Sergey Alexandrovich Kryukov 14-Aug-14 2:40am
The performance of this solution would be not so good. It's much better to find a split point without creating of any objects, and then split in two Substring operations. The algorithm will be very simple; I would leave it for OP's home exercise... :-)
—SA
Abhinav S 14-Aug-14 2:44am
Yes true.
[no name] 14-Aug-14 2:50am
Thanks for suggestion.
It was urgent so i thought experts in this forum will help me out so i asked question here.

## Solution 4

This code is based on previous solutions and ideas, just if someone needs a "ready-to-use" one... ;)
C#
```public static class StringExtensions
{
public static List<string> SplitAtOccurence(this string input, char separator, int occurence)
{
var parts = input.Split(separator);
var partlist = new List<string>();
var result = new List<string>();
for (int i = 0; i < parts.Length; i++)
{
if (partlist.Count == occurence)
{
partlist.Clear();
}
if (i == parts.Length - 1) result.Add(string.Join(separator.ToString(), partlist)); // if no more parts, add the rest
}
return result;
}
}```

The usage (in the same namespace) is something like this:
C#
`"1,2,3,4,5,6,7,8,9,10".SplitAtOccurence(',', 4);`

This returns a List, with the items: "1,2,3,4", "5,6,7,8" and "9,10"

v2

## Solution 5

If you know there are always 30 values, then it is pretty trivial with Regex:
C#
```string input = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";
var matches = Regex.Match(input, @"^((?:\d+,){9}\d+),((?:\d+,){9}\d+),((?:\d+,){9}\d+)\$");
// matches.Groups[1]  has "1,2,3,4,5,6,7,8,9,10"
// matches.Groups[2]  has "11,12,13,14,15,16,17,18,19,20"
// matches.Groups[3]  has "21,22,23,24,25,26,27,28,29,30"```

You can work out the variations from there... ;-)

_Asif_ 27-Jan-15 8:16am
I think that is great solution, i thought the same :) +5

## Solution 2

C#
```static void Main(string[] args)
{

var finalResult = GetSplittedNumbers("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30");
}
public static List<string> GetSplittedNumbers(string stringSeparatedByComma)
{
var listOfSeparatedValues = new List<string>();
List<int> splittedIntArrays =
stringSeparatedByComma.Split(',').Select(
x => Convert.ToInt32(x)).ToList();
var splittedList = new List<int>();
foreach (int item in splittedIntArrays)
{

int status = item % 10;
if (splittedList == null)
{
splittedList = new List<int>();
}

if (status == 0)
{

string res = AgainCommaSeparated(splittedList);
splittedList = null;
}
}
return listOfSeparatedValues;
}
private static string AgainCommaSeparated(IEnumerable<int> numbers )
{
var result= numbers.Aggregate("", (current, number) => current + number.ToString() + ",");
return result.Substring(0, result.Length - 1);
}
```

Hope this helps

v5

## Solution 3

You can try like this:

C#
```string number = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";
string[] tempNumber = number.Split(',');
string[] finalResult = new string[5];
int j = 0;
int k = 0;
string x = string.Empty;
for (int i = 0; i < tempNumber.Length; i++)
{
x = x + "," + tempNumber[i];
k++;
if (k == 10)
{
x = x.Remove(0, 1);
finalResult[j] = x;
x = string.Empty;
j++;
k = 0;
}
}
```

v2

## Solution 6

You can get the solution by using below methods and passing the parameter as string and splitIndex. I tried to keep the solution simple and short. Hope it will help you.

C#
```static void Main(string[] args)
{
var inputString = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";

string[] resultArr = StringSpliterNthOccourance(inputString, 5);
}

private static string[] StringSpliterNthOccourance(string inputString, int splitIndex)
{
string[] resultArr = null;
if (inputString.Length > 0 && inputString.Length > splitIndex)
{
string[] stringArr = inputString.Split(',');

int Skipindex = 0;
int endIndex = stringArr.Length / splitIndex;
resultArr = new string[endIndex];
for (int i = 0; i < endIndex; i++)
{
resultArr[i] = string.Join(",", stringArr.Skip(Skipindex).Take(splitIndex).ToArray());
Skipindex += splitIndex;
}
}
return resultArr;
}```