[英]“System”-code to “System.Linq”-code
I am incredibly new to LINQ, in fact... I'm so new I just figured out that everything before the dot is given through to the called method. 实际上,我对LINQ来说是个不可思议的新手,我是如此新奇,我才发现,点之前的所有内容都被赋予了被调用的方法。
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(SongDecoder("WUBWUBABCWUB"));
Console.WriteLine(SongDecoder("RWUBWUBWUBLWUB"));
}
public static string SongDecoder(string input)
{
string[] s = input.Split(new string[] { "WUB" }, StringSplitOptions.RemoveEmptyEntries);
string reStr = "";
for (int i = 0; i < s.Length; i++)
if(i == s.Length - 1)
reStr += s[i];
else
reStr += s[i] + " ";
return reStr;
}
}
I'm wondering how I can convert this to a "simple" LINQ variant and if it'd be faster with LINQ (As I heard great and fast things about LINQ.). 我想知道如何将其转换为“简单的” LINQ变体,以及使用LINQ是否会更快(因为我听说过有关LINQ的好又快的知识。)。
No LINQ needed here 这里不需要LINQ
var reStr = String.Join(" ",
input.Split(new string[] {"WUB"}, StringSplitOptions.RemoveEmptyEntries));
Sorry :( LINQ is very useful though, I suggest you read about it. 抱歉:(但是LINQ非常有用,我建议您阅读一下。
Ok, I conceed, if you really want to use LINQ there is always Aggregate
好的,我认为,如果您真的想使用LINQ,总会有Aggregate
var retStr = input.Split(new string[] {"WUB"}, StringSplitOptions.RemoveEmptyEntries))
.Aggregate ( (a,b) => a + " " + b);
read more here: LINQ Aggregate algorithm explained 在这里阅读更多: LINQ Aggregate算法解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.