Convert list to number range string

后端 未结 6 611
你的背包
你的背包 2020-12-05 19:45

This question is pretty much the opposite of this question: Does C# have built-in support for parsing page-number strings?

So given

1,3,5,6,7,8,9,10         


        
6条回答
  •  伪装坚强ぢ
    2020-12-05 20:34

    Had to solve same problem. Was finding alternatives to my solution which I think looks more logical. Therefore sharing it. Set second parameter to true if you want to sort an unsorted list.

    public string ToRangeString(List list, bool withSort) {
      list = list.Distinct().ToList();
      if(withSort) list.Sort();
    
      StringBuilder result = new StringBuilder();
      int temp;
    
      for (int i=0; i

提交回复
热议问题