Совершенствование @ andrius-naruševičius очень полезного ответа, чтобы сделать его более понятным и легко настраиваемым
/// <summary>
/// /5981141/suschestvuet-li-tip-c-dlya-predstavleniya-tselochislennogo-diapazona
/// </summary>
public class Range
{
readonly static char[] Separators = {','};
public static List<int> Explode(int from, int to)
{
return Enumerable.Range(from, (to-from)+1).ToList();
}
public static List<int> Interpret(string input)
{
var result = new List<int>();
var values = input.Split(Separators);
string rangePattern = @"(?<range>(?<from>\d+)-(?<to>\d+))";
var regex = new Regex(rangePattern);
foreach (string value in values)
{
var match = regex.Match(value);
if (match.Success)
{
var from = Parse(match.Groups["from"].Value);
var to = Parse(match.Groups["to"].Value);
result.AddRange(Explode(from, to));
}
else
{
result.Add(Parse(value));
}
}
return result;
}
/// <summary>
/// Split this out to allow custom throw etc
/// </summary>
private static int Parse(string value)
{
int output;
var ok = int.TryParse(value, out output);
if (!ok) throw new FormatException($"Failed to parse '{value}' as an integer");
return output;
}
}
и тесты:
[Test]
public void ExplodeRange()
{
var output = Range.Explode(5, 9);
Assert.AreEqual(5, output.Count);
Assert.AreEqual(5, output[0]);
Assert.AreEqual(6, output[1]);
Assert.AreEqual(7, output[2]);
Assert.AreEqual(8, output[3]);
Assert.AreEqual(9, output[4]);
}
[Test]
public void ExplodeSingle()
{
var output = Range.Explode(1, 1);
Assert.AreEqual(1, output.Count);
Assert.AreEqual(1, output[0]);
}
[Test]
public void InterpretSimple()
{
var output = Range.Interpret("50");
Assert.AreEqual(1, output.Count);
Assert.AreEqual(50, output[0]);
}
[Test]
public void InterpretComplex()
{
var output = Range.Interpret("1,5-9,14,16,17,20-24");
Assert.AreEqual(14, output.Count);
Assert.AreEqual(1, output[0]);
Assert.AreEqual(5, output[1]);
Assert.AreEqual(6, output[2]);
Assert.AreEqual(7, output[3]);
Assert.AreEqual(8, output[4]);
Assert.AreEqual(9, output[5]);
Assert.AreEqual(14, output[6]);
Assert.AreEqual(16, output[7]);
Assert.AreEqual(17, output[8]);
Assert.AreEqual(20, output[9]);
Assert.AreEqual(21, output[10]);
Assert.AreEqual(22, output[11]);
Assert.AreEqual(23, output[12]);
Assert.AreEqual(24, output[13]);
}
[ExpectedException(typeof (FormatException))]
[Test]
public void InterpretBad()
{
Range.Interpret("powdered toast man");
}