# Calculate number of pages in page range

I’m looking for a way to easily calculate the number of pages in a page range such as the below:

Input: “1, 3-5, 9-11, 25”
(Desired) Output: 8 (the total number of pages within the range)

If there is any pre-built logic, that would be ideal. Otherwise I’d have to build from scratch.

Hi,

``````yourString.Split(","c).Select(Function(s) if (s.Contains("-"),Int32.Parse(s.Split("-"c)(1).Trim)-Int32.Parse(s.Split("-"c)(0).Trim)+1,1)).Sum()
``````

Regards,

1 Like

That’ll work, thanks!

1 Like

could you please explain this string Input: “1, 3-5, 9-11, 25”

Hey @Aleem_Khan

That string shows the range

1
3-5 means 3,4,5
9-11 means 9,10,11
25

This shows there are 8 elements in that range group.

Requirement from @Greg_Jacobson was to calculate those.

@Yoichi solutions is to check if there’s `-` if yes split and calculate the difference (subtraction) of those and add 1 (5-3 = 2, if we add 1 then only it becomes 3, which is the correct number of elements in that range) , if not then simply count it as one.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.