将字符串数组合在一起

时间:2011-10-04 09:50:16

标签: c# .net arrays linq

我希望将两个字符串数组的内容组合成一个新列表,其中两个内容都连接在一起。

string[] days = { "Mon", "Tue", "Wed" };
string[] months = { "Jan", "Feb", "Mar" };

// I want the output to be a list with the contents
// "Mon Jan", "Mon Feb", "Mon Mar", "Tue Jan", "Tue Feb" etc...

我该怎么办?因为当它只有两个数组时,以下工作并且很容易:

List<string> CombineWords(string[] wordsOne, string[] wordsTwo)
{
    var combinedWords = new List<string>();
    foreach (var wordOne in wordsOne)
    {
        foreach (string wordTwo in wordsTwo)
        {
            combinedWords.Add(wordOne + " " + wordTwo);
        }
    }
    return combinedWords;
}

但我希望能够传递不同数量的数组(即使用下面的签名方法)并让它仍然有效。

List<string> CombineWords(params string[][] arraysOfWords)
{
    // what needs to go here ?
}

或者其他一些解决方案会很棒。如果只使用Linq可以做到这一点,那就更好了!

6 个答案:

答案 0 :(得分:16)

你想要做的事实上是所有单词数组的笛卡尔积,然后用空格加入单词。 Eric Lippert简单地实现了Linq笛卡尔积[{3}}。您可以使用它来实现CombineWords:

List<string> CombineWords(params string[][] arraysOfWords)
{
    return CartesianProduct(arraysOfWords)
            .Select(x => string.Join(" ", x))
            .ToList();
}

答案 1 :(得分:4)

在任意数量的字符串数组上交叉连接:

// Define other methods and classes here
List<string> CombineWords(params string[][] arraysOfWords)
{
    if (arraysOfWords.Length == 0)
        return new List<string>();

    IEnumerable<string> result = arraysOfWords[0];

    foreach( string[] words in arraysOfWords.Skip(1) )
    {
        var tempWords = words;

        result = from r in result
                 from w in tempWords 
                 select string.Concat(r, " ", w);
    }

    return result.ToList();
}

答案 2 :(得分:2)

public static List<string> CombineWords(params string[][] arraysOfWords)
{
    var strings = new List<string>();

    if (arraysOfWords.Length == 0)
    {
        return strings;
    }

    Action<string, int> combineWordsInternal = null;

    combineWordsInternal = (baseString, index) =>
    {
        foreach (var str in arraysOfWords[index])
        {
            string str2 = baseString + " " + str;

            if (index + 1 < arraysOfWords.Length)
            {
                combineWordsInternal(str2, index + 1);
            }
            else
            {
                strings.Add(str2);
            }
        }
    };

    combineWordsInternal(string.Empty, 0);

    return strings;
}

第二次尝试......我无法在LINQ中执行此操作...正确地使用一点太复杂了: - )

我正在使用本地匿名函数(并且表明在匿名函数上递归非常复杂,因为你必须单独声明它们)

答案 3 :(得分:2)

以下代码适用于任意数量的数组(并在某种程度上使用linq):

List<string> CombineWords(params string[][] wordsToCombine)
{
     if (wordsToCombine.Length == 0)
         return new List<string>();

     IEnumerable<string> combinedWords = wordsToCombine[0].ToList();
     for (int i = 1; i < wordsToCombine.Length; ++i)
     {
         var temp = i;
         combinedWords = (from x in combinedWords from y in wordsToCombine[temp]
                       select x + " " + y);
     }
     return combinedWords.ToList();
 }

答案 4 :(得分:1)

这是一个非递归解决方案,它在进展时缓冲字符串,以减少连接数。因此它也应该可用于更多阵列。 它还保留了您想要的顺序 - 第一个数组中的项目将始终位于结果字符串的开头。

  var s1 = new string[] { "A", "B", "C" };
  var s2 = new string[] { "1", "2", "3", "4" };
  var s3 = new string[] { "-", "!", "?" };
  var res = Combine(s1, s2, s3);

有问题的功能:

private List<string> Combine(params string[][] arrays)
{
    if (arrays.Length == 1)
    {
        // The trivial case - exit.
        return new List<string>(arrays[0]);
    }
    IEnumerable<string> last = arrays[arrays.Length - 1];
    // Build from the last array, progress forward
    for (int i = arrays.Length - 2; i >= 0; i--)
    {
        var buffer = new List<string>();
        var current = arrays[i];
        foreach (var head in current)
        {
            foreach (var tail in last)
            {
                // Concatenate with the desired space.
                buffer.Add(head + " " + tail);
            }
        }
        last = buffer;
    }
    return (List<string>)last;
}

答案 5 :(得分:0)

你能试试这个方法吗?

static List<string> CombineWords(string[] wordsOne, string[] wordsTwo)
{
    var combinedWords = new List<string>();
    for(int x = 0; (x <= wordsOne.Length - 1); ++x)
    {
        for(int y = 0; (x <= wordsTwo.Length - 1); ++y)
        {
            combinedWords.Add(string.Format("{0} {1}", wordsOne[x], wordsTwo[y]));
        }
    }
    return combinedWords;
}

克里斯