如何将字符串数组转换为句子?

时间:2014-03-02 20:21:37

标签: c#

如何使用Linq查询方法将字符串数组转换为句子?

private static void Main()
{
    string sentence = "C# is fun.";
    string[] words = sentence.Split();
    //string temp= words;       
}

temp希望与sentence具有相同的价值。

4 个答案:

答案 0 :(得分:6)

您可以使用

var res = string.Join(" ", words);

string[] words = { "one", "two", "three" };
var res = words.Aggregate((current, next) => current + " " + next);

答案 1 :(得分:4)

您可以尝试:

var temp = words.Aggregate((x, y) => x + " " + y);

答案 2 :(得分:3)

string temp = words.Aggregate((workingSentence, next) => 
      workingSentence + " " + next);

参考:http://msdn.microsoft.com/en-us/library/bb548651%28v=vs.110%29.aspx

答案 3 :(得分:2)

使用String.Join方法:

private static void Main()
{
    string sentence = "C# is fun.";
    string[] words = sentence.Split();

    // Join the words back together, with a " " in between each one.
    string temp = String.Join(" ", words);
}