Reputation: 9277
I have the following string in C#:
string a="1,2,3,4,5,6,7,8,9,10,22,45,66,88,100,101,102,103,104,105,140,199";
I would like to generate the following output:
1-10,22,45,66,88,100-105,140,199 grouping the consecutive numbers into ranges on this way
"initialnumber-endnumber" and leaving the others as it is.
Could you give me a hand on this?
Thanks in advance.
Upvotes: 2
Views: 988
Reputation: 35756
okay, first step, enumerate string as ordered integers.
var numbers = a.Split(',').Select(int.Parse()).OrderBy(i => i);
So, then we could use these two functions to enumerate the ranges like this.
private static string RangeToString(int first, int last)
{
if ( last != first )
{
return string.Format("{0}-{1}", first, last);
}
else
{
return last.ToString();
}
}
private static IEnumerable<string> GetRanges(IEnumerable<int> numbers)
{
var last = numbers.First();
var first = last;
foreach(var number in numbers.Skip(1))
{
if (lastNumber + 1 != number)
{
yield return RangeToString(first, last);
first = number;
}
last == number;
}
yield return RangeToString(first, last);
}
Then we can put it together like this,
public static string GetRangedList(string list)
{
var numbers = list.Split(',').Select(int.Parse()).OrderBy(i => i);
return string.Join(",", GetRanges(numbers));
}
Which obviously you would call like this
var b = GetRangedList(a);
Upvotes: 1
Reputation: 888293
IEnumerable<string> Rangify(IList<int> input) {
for (int i = 0; i < input.Count; ) {
var start = input[i];
int size = 1;
while (++i < input.Count && input[i] == start + size)
size++;
if (size == 1)
yield return start.ToString();
else if (size == 2) {
yield return start.ToString();
yield return (start + 1).ToString();
} else if (size > 2)
yield return start + " - " + (start + size - 1);
}
}
Upvotes: 6