将字符串数组拆分为多个部分

本文关键字:个部 拆分 字符串 数组 | 更新日期: 2023-09-27 17:51:11

我可以half-@ss,但是我想要一种干净的方式来做这件事,这样就不会给以后的处理带来任何麻烦。

private String[][] SplitInto10(string[] currTermPairs)
{
   //what do i put in here to return 10 string arrays
   //they are all elements of currTermPairs, just split into 10 arrays.
}

所以我基本上想把一个字符串数组(currTermPairs)平均分成10或11个不同的字符串数组。我需要确保没有数据丢失,所有元素都成功传输

编辑:给你一个n大小的字符串数组。需要发生的是该方法需要从给定的字符串数组返回10个字符串数组/列表。换句话说,将数组分成10个部分。

例如,如果我有

 A B C D E F G H I J K L M N O P Q R S T U

我需要根据大小将其分成10个字符串数组或11个字符串数组,因此在本例中,我将使用

A B
C D
E F
G H 
I J
K L
M N 
O P 
Q R 
S T 
U   <--Notice this is the 11th array and it is the remainder

将字符串数组拆分为多个部分

使用剩余的%运算符代替,这里使用Linq方法:

string[][] allArrays = currTermPairs
            .Select((str, index) => new { str, index })
            .GroupBy(x => x.index % 10)
            .Select(g => g.Select(x => x.str).ToArray())
            .ToArray();

Demo(每个数组有2个字符串)

这是一个不使用LINQ的解决方案,如果你想习惯数组和for循环:

// Determine the number of partitions.
int parts = currTermPairs.Length < 10 ? currTermPairs.Length : 10;
// Create the result array and determine the average length of the partitions.
var result = new string[parts][];
double avgLength = (double)currTermPairs.Length / parts;
double processedLength = 0.0;
int currentStart = 0;
for (int i = 0; i < parts; i++) {
    processedLength += avgLength;
    int currentEnd = (int)Math.Round(processedLength);
    int partLength = currentEnd - currentStart;
    result[i] = new string[partLength];
    Array.Copy(currTermPairs, currentStart, result[i], 0, partLength);
    currentStart = currentEnd;
}
return result;

项目的总数可能不能被10整除。问题是零件的不同长度将如何分布。这里我试着平均分配它们。注意铸造(double)currTermPairs.Length。为了得到浮点除法而不是整数除法,这是必要的。

这里有一个小测试方法:

const int N = 35;
var arr = new string[N];
for (int i = 0; i < N; i++) {
    arr[i] = i.ToString("00");
}
var result = new PatrtitioningArray().SplitInto10(arr);
for (int i = 0; i < result.Length; i++) {
    Console.Write("{0}:   ", i);
    for (int k = 0; k < result[i].Length; k++) {
        Console.Write("{0}, ", result[i][k]);
    }
    Console.WriteLine();
}

它的输出是(包含35个元素):

0:   00, 01, 02, 03, 
1:   04, 05, 06, 
2:   07, 08, 09, 
3:   10, 11, 12, 13, 
4:   14, 15, 16, 17, 
5:   18, 19, 20, 
6:   21, 22, 23, 
7:   24, 25, 26, 27, 
8:   28, 29, 30, 31, 
9:   32, 33, 34, 

我会说创建一个包含10或11(无论您实际想要的数字)List<string> s的List<List<string>>,并做这样的事情:

int i = 0;
int index;
foreach(string s in src)
{
  index = i % lists.Length; //lists is the List<List<string>>
  lists[index].Add(s);
  i++;
}

当然,只有在原始列表中至少有10或11个项目时,您才能将其拆分为10或11个列表。

下面的帖子展示了一个拆分数组的好例子:

c#拆分数组

包含自定义分割和中点分割。

这可以将它们按顺序排列(即{1,2},{3,4},{5,6},{7,8},{9,10},{11,12},{13,14},{15,16},{17,18},{19,20},{21}):

    int groupSize = items.Length / 10;
    string[][] sets = items.Select((str, idx) => new { index = idx, value = str })
                           .GroupBy(a => a.index / groupSize)
                           .Select(gr => gr.Select(n => n.value).ToArray())
                           .ToArray();

如果你有102个项目,这将给你10个包含10个项目的数组,一个包含2个项目的数组(剩余部分)。这是你所期待的吗?

使用MoreLinq的批量扩展方法:

private String[][] SplitIntoParts(string[] items, int equalPartsCount)
{
   var batches = items.Batch(items.Count() / equalPartsCount);
   return batches.Select(x => x.ToArray()).ToArray();
}