加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

什么是Perl重复运算符的C#等价物?

发布时间:2020-12-15 21:52:05 所属栏目:大数据 来源:网络整理
导读:在Perl print "a" x 3; # aaa 在C#中 Console.WriteLine( ??? ) 解决方法 这取决于你需要什么…例如有新的字符串(‘a’,3). 用于处理字符串;你可以循环…不是很有趣,但它会工作. 使用3.5,您可以使用Enumerable.Repeat(“a”,3),但这会为您提供一系列字符串,
在Perl
print "a" x 3;  # aaa

在C#中

Console.WriteLine( ??? )

解决方法

这取决于你需要什么…例如有新的字符串(‘a’,3).

用于处理字符串;你可以循环…不是很有趣,但它会工作.

使用3.5,您可以使用Enumerable.Repeat(“a”,3),但这会为您提供一系列字符串,而不是复合字符串.

如果您打算使用它,可以使用定制的C#3.0扩展方法:

static void Main()
    {
        string foo = "foo";
        string bar = foo.Repeat(3);
    }
    // stuff this bit away in some class library somewhere...
    static string Repeat(this string value,int count)
    {
        if (count < 0) throw new ArgumentOutOfRangeException("count");
        if (string.IsNullOrEmpty(value)) return value; // GIGO            
        if (count == 0) return "";
        StringBuilder sb = new StringBuilder(value.Length * count);
        for (int i = 0; i < count; i++)
        {
            sb.Append(value);
        }
        return sb.ToString();
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读