Does C# have built-in support for parsing strings of page numbers? By page numbers, I mean the format you might enter into a print dialog that's a mixture of comma and dash-delimited.
Something like this:
1,3,5-10,12
What would be really nice is a solution that gave me back some kind of list of all page numbers represented by the string. In the above example, getting a list back like this would be nice:
1,3,5,6,7,8,9,10,12
I just want to avoid rolling my own if there's an easy way to do it.
Here's something I cooked up for something similar.
It handles the following types of ranges:
It does not check for duplicate values, so the set 1,5,-10 will produce the sequence 1, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
My solution:
autocomplete: 1,-3,5-,8 (Nmax=9) => 1,3,5,6,7,8,9,8
Regex is not efficient as following code. String methods are more efficient than Regex and should be used when possible.
Here's a slightly modified version of lassevk's code that handles the string.Split operation inside of the Regex match. It's written as an extension method and you can easily handle the duplicates problem using the Disinct() extension from LINQ.
Below is the code I just put together to do this.. You can enter in the format like.. 1-2,5abcd,6,7,20-15,,,,,,
easy to add-on for other formats
One line approach with
Split
andLinq