Does C# have built-in support for parsing page-number strings?

前端 未结 11 967
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 18:09

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 das

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 18:58

    One line approach with Split and Linq

    string input = "1,3,5-10,12";
    IEnumerable result = input.Split(',').SelectMany(x => x.Contains('-') ? Enumerable.Range(int.Parse(x.Split('-')[0]), int.Parse(x.Split('-')[1]) - int.Parse(x.Split('-')[0]) + 1) : new int[] { int.Parse(x) });
    

提交回复
热议问题