C# 如何将字符串数组转换为句子?
如何使用Linq查询方法将字符串数组转换为句子C# 如何将字符串数组转换为句子?,c#,C#,如何使用Linq查询方法将字符串数组转换为句子 private static void Main() { string sentence = "C# is fun."; string[] words = sentence.Split(); //string temp= words; } temp希望与语句具有相同的值 string temp = words.Aggregate((workingSentence, next) => wor
private static void Main()
{
string sentence = "C# is fun.";
string[] words = sentence.Split();
//string temp= words;
}
temp
希望与语句
具有相同的值
string temp = words.Aggregate((workingSentence, next) =>
workingSentence + " " + next);
参考:您可以尝试:
var temp = words.Aggregate((x, y) => x + " " + y);
你可以用
var res = string.Join(" ", words);
或
使用以下方法:
LINQ在这里有什么用?@rendon:只是为了学习LINQ。
String.Join
是一个更好的选择。不要仅仅为了使用LINQ而使用LINQ。为什么,为什么,为什么单词。ToArray
?它已经是一个数组了有什么意义?即使不是,string.Join也将IEnumerable
作为参数,因此您不需要将其转换为列表或数组。这里的唯一答案提供了一个应在实际代码中使用的示例。
string[] words = { "one", "two", "three" };
var res = words.Aggregate((current, next) => current + " " + next);
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);
}