的string.join(字符串,字符串[])为IEnumerable的&LT的模拟; T> [英] An analog of String.Join(string, string[]) for IEnumerable<T>

查看:134
本文介绍了的string.join(字符串,字符串[])为IEnumerable的&LT的模拟; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串包含非常有用的方法。 - 的string.join(字符串,字符串[])

class String contains very useful method - String.Join(string, string[]).

有从阵列创建一个串,分离具有给定的码元阵列的每个元素。但一般 - 它不会在最后一个元素之后添加一个分隔符!我将它用于ASP.NET编码具有分离< BR /> Environment.NewLine

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.

所以,我想在每一行后添加一个空行ASP:表。用什么方法的IEnumerable<的;的TableRow> 我可以使用相同的功能。

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?

推荐答案

我写了一个扩展方法:

    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())

这篇关于的string.join(字符串,字符串[])为IEnumerable的&LT的模拟; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆