Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Perl的C等价物是什么;s重复运算符?_C#_Perl_String - Fatal编程技术网

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

C# Perl的C等价物是什么;s重复运算符?,c#,perl,string,C#,Perl,String,在Perl中 在C# 这取决于你需要什么。。。例如,有新字符串('a',3) 用于处理字符串;你可以循环。。。不是很有趣,但会有用的 在3.5中,您可以使用Enumerable.Repeat(“a”,3),但这会提供一个字符串序列,而不是一个复合字符串 如果要经常使用此功能,可以使用定制的C#3.0扩展方法: Console.WriteLine( ??? ) static void Main() { 字符串foo=“foo”; 字符串条=foo.重复(3); } //把这些东西放在某个类库的某

在Perl中

在C#


这取决于你需要什么。。。例如,有
新字符串('a',3)

用于处理字符串;你可以循环。。。不是很有趣,但会有用的

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

如果要经常使用此功能,可以使用定制的C#3.0扩展方法:

Console.WriteLine( ??? )
static void Main()
{
字符串foo=“foo”;
字符串条=foo.重复(3);
}
//把这些东西放在某个类库的某个地方。。。
静态字符串重复(此字符串值,整数计数)
{
如果(计数<0)抛出新ArgumentOutOfRangeException(“计数”);
if(string.IsNullOrEmpty(value))返回值;//GIGO
如果(计数=0)返回“”;
StringBuilder sb=新StringBuilder(value.Length*count);
for(int i=0;i
如果您只需要重复一个字符(如示例中所示),则这将起作用:

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

如果需要像Tom指出的那样使用字符串,那么扩展方法将很好地完成这项工作

Console.WriteLine(new string('a', 3))

在所有版本的.NET中,重复一个字符串总是可以这样做的

static class StringHelpers
{
    public static string Repeat(this string Template, int Count)
    {
        string Combined = Template;
        while (Count > 1) {
            Combined += Template;
            Count--;
        }
        return Combined;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string s = "abc";
        Console.WriteLine(s.Repeat(3));
        Console.ReadKey();
    }

是的,但它满足了问题中的要求。那么Perl的重复运算符只对字符有效?他使用了一个由单个字符组成的字符串,这一事实不应该让你把答案局限于那个特定的例子。这个问题更一般。请参阅我的其他回答,了解如何使用字符串执行此操作。对于这样的循环中的工作,建议使用StringBuilder。。。串联对于2或3是好的,但是如果有人要求“a”。重复(100),你有很多不必要的字符串要收集。这是真的。StringBuilder在某些方面会变得更加高效。我听说大约10点。在给出的例子中,他有3个,所以我假设它很小。如果你想覆盖更广的范围,那么输入一个“如果”并使用StringBuilder超过10次会更有效。你不认为StringBuilder().Insert(0,value,count)更好吗?
static class StringHelpers
{
    public static string Repeat(this string Template, int Count)
    {
        string Combined = Template;
        while (Count > 1) {
            Combined += Template;
            Count--;
        }
        return Combined;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string s = "abc";
        Console.WriteLine(s.Repeat(3));
        Console.ReadKey();
    }
public static string Repeat(string value, int count)
{
  return new StringBuilder().Insert(0, value, count).ToString();
}