可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
class String
contains very useful method - String.Join(string, string[])
.
It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with "<br />
" or Environment.NewLine
.
So I want to add an empty row after each row in asp:Table
. What method of IEnumerable<TableRow>
can I use for the same functionality?
回答1:
I wrote an extension method:
public static IEnumerable<T>
Join<T>(this IEnumerable<T> src, Func<T> separatorFactory)
{
var srcArr = src.ToArray();
for (int i = 0; i < srcArr.Length; i++)
{
yield return srcArr[i];
if(i<srcArr.Length-1)
{
yield return separatorFactory();
}
}
}
You can use it as follows:
tableRowList.Join(()=>new TableRow())
回答2:
The Linq equivalent of String.Join
is Aggregate
For instance:
IEnumerable<string> strings;
string joinedString = strings.Aggregate((total,next) => total + ", " + next);
If given an IE of TableRows, the code will be similar.
回答3:
In .NET 3.5 you can use this extension method:
public static string Join<TItem>(this IEnumerable<TItem> enumerable, string separator)
{
return string.Join(separator, enumerable.Select(x => x.ToString()).ToArray());
}
or in .NET 4
public static string Join<TItem>(this IEnumerable<TItem> enumerable, string separator)
{
return string.Join(separator, enumerable);
}
BUT the question wanted a separator after each element including the last for which this (3.5 version) would work:-
public static string AddDelimiterAfter<TItem>(this IEnumerable<TItem> enumerable, string delimiter)
{
return string.Join("", enumerable.Select(x => x.ToString() + separator).ToArray());
}
You could also use .Aggregate to do this without an extension method.
回答4:
If I couldn't find a method that suits my need, I would just create my own. And extension methods are very nice that way since they let you extend stuff like that. Don't know much about asp:table, but here is an extension method at least which you can tweak to whatever :p
public static class TableRowExtensions
{
public string JoinRows(this IEnumerable<TableRow> rows, string separator)
{
// do what you gotta do
}
}
回答5:
What you are looking for is an Intersperse
function. For LINQ implementations of such a function, see this question.
Incidentally, another possible analog of String.Join
is the Intercalate
function, which is actually what I was looking for:
public static IEnumerable<T> Intercalate<T>(this IEnumerable<IEnumerable<T>> source,
IEnumerable<T> separator) {
if (source == null) throw new ArgumentNullException("source");
if (separator == null) throw new ArgumentNullException("separator");
return source.Intersperse(separator)
.Aggregate(Enumerable.Empty<T>(), Enumerable.Concat);
}
回答6:
There is no built in method to do that, you should roll your own.
回答7:
If you are going to do this sort of thing frequently, then it's worth building your own extension method to do it. The implementation below allows you to do the equivalent of string.Join(", ", arrayOfStrings)
where the arrayOfStrings can be an IEnumerable<T>
, and separator can be any object at all. It allows you to do something like this:
var names = new [] { "Fred", "Barney", "Wilma", "Betty" };
var list = names
.Where(n => n.Contains("e"))
.Join(", ");
Two things I like about this are:
- It's very readable in a LINQ context.
- It's fairly efficient because it uses StringBuilder and avoids evaluating the enumeration twice.
public static string Join<TItem,TSep>(
this IEnumerable<TItem> enuml,
TSep separator)
{
if (null == enuml) return string.Empty;
var sb = new StringBuilder();
using (var enumr = enuml.GetEnumerator())
{
if (null != enumr && enumr.MoveNext())
{
sb.Append(enumr.Current);
while (enumr.MoveNext())
{
sb.Append(separator).Append(enumr.Current);
}
}
}
return sb.ToString();
}