代码之家  ›  专栏  ›  技术社区  ›  Shailendra Kumar

从索引范围的字符串列表中联接字符串

  •  0
  • Shailendra Kumar  · 技术社区  · 8 年前

    我有一个很大的字符串列表,我需要另一个包含字符串的字符串列表。用逗号连接前300个索引,然后再连接300个索引,依此类推。直到所有字符串完成。 我在这里展示我所做的一切。

        List<string> tempSMSNoList = SMSNos.Split(',').ToList(); // 938
           if (tempSMSNoList.Count > 300)
            {
               int Quotient = tempSMSNoList.Count / 300;   // 3
               int Remainder = tempSMSNoList.Count % 300;  // 38
    
               List<string> myNewString = new List<string>(); // Need to store new value in this list.
               for(int i=0; i < Quotient; i++)
               {
                 // Logic here..
               }
            }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Orel Eraki    8 年前

    首先,您的代码不应该编译,因为 Quotient 是一个 int 类型而不是类包含 Count 财产。

    第二,有一个林肯 Skip 和 Take 可以帮助创建新值集合的方法。

    int groupSize = 300;
    var numberOfGroups = Quotient + (Remainder > 0 ? 1 : 0); // If there is any remainder then add it as another loop.
    for (int i = 0; i < numberOfGroups; i++)
    {
        List<string> myNewString = tempSMSNoList.Skip(i*groupSize).Take(groupSize).ToList();
    }
    
        2
  •  0
  •   derpirscher    8 年前

    你可以用林肯来做这个

    var result = new List<string>();
    int groupsize = 300;
    for (int i = 0; i< tempSMSNoList.Count(); i+= groupsize)
        result.Add(String.Join(",", tempSMSNoList.Skip(i).Take(groupsize));