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

在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();
    }

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...