如何修剪 List那么前面和后面的空白行被删除了吗?
最简单的方法是什么?
结果应该是:
1: one
2: two
3:
4:
5: five
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLines8833
{
class Program
{
static void Main(string[] args)
{
List<string> lines = new List<string>();
lines.Add("");
lines.Add("one");
lines.Add("two");
lines.Add("");
lines.Add("");
lines.Add("five");
lines.Add("");
lines.Add("");
lines.TrimList();
}
}
public static class Helpers
{
public static List<string> TrimList(this List<string> list)
{
//???
}
}
}
What is the easiest way to do this?
The results should be:
1: one
2: two
3:
4:
5: five
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLines8833
{
class Program
{
static void Main(string[] args)
{
List<string> lines = new List<string>();
lines.Add("");
lines.Add("one");
lines.Add("two");
lines.Add("");
lines.Add("");
lines.Add("five");
lines.Add("");
lines.Add("");
lines.TrimList();
}
}
public static class Helpers
{
public static List<string> TrimList(this List<string> list)
{
//???
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好的,现在我明白了想要的结果:
请注意,这不会更改现有列表 - 它返回一个包含所需内容的新列表。鉴于您已为该方法指定了返回类型,但您的示例在不使用结果的情况下调用了它,因此尚不清楚您想要什么行为。就我个人而言,我更喜欢无副作用的方法,尽管可能值得更改名称:)
Okay, now I understand the desired results:
Note that this doesn't change the existing list - it returns a new list with the desired content. It wasn't clear exactly what behaviour you wanted, given that you've given the method a return type, but your sample calls it without using the result. Personally I prefer non-side-effecting methods, although it may be worth changing the name :)
怎么样:
请注意,签名已从您的示例中更改(返回类型为 void)。
What about this:
Note that the signature has changed from your example (return type is void).
试试这个:
Try this one:
我知道老问题,但这里有一个扩展方法,它将使用 Linq 基于布尔委托从集合的开头和结尾修剪对象。
您的案例示例:
Old Question I know, but here is an extension method that will trim objects from the beginning and end of a collection based on a boolean delegate using Linq.
Example for your case:
如果您想删除空字符串,您可以执行以下操作...
更新: 抱歉,刚刚看到您的编辑,您希望保留内部空白。
在这种情况下,我只想使用一种扩展方法,就像您上面提到的那样,因为我认为没有更简单的方法可以做到这一点。
If you wanted to remove the empty strings you could do something like this...
Update: Sorry just seen your edit that you want internal blanks to remain.
In this case I would just an extension method like the one you mention above as I don't think there is a simpler way of doing it.
没有内置任何东西可以执行类似的特定操作。您可以从头到尾检查项目并删除空字符串。为了最大限度地减少对列表的操作(重复使用RemoveAt删除第一个项目效率相当低),首先计算要删除的项目数量,然后使用
RemoveRange
方法一次性删除它们。为了匹配您在代码中使用该方法的方式,扩展必须更改列表而不是返回新列表。
There is nothing built in to do something specific like that. You can check the items from the beginning and end and remove the empty strings. To minimise the operations on the list (using RemoveAt repeatedly to remove the first item is rather inefficient), first count the number of items to remove and then use the
RemoveRange
method to remove them all at once.To match how you use the method in the code, the extension has to alter the list rather than returning a new list.
有时,从可读性和性能角度来看,老旧的 foreach 都胜过 linq:
Sometime the good old foreach beats linq both from the readability and performance perspective: