我试图在基于项深度的字符串之前插入一定数量的缩进,我想知道是否有一种方法可以返回一个重复X次的字符串。例子:

string indent = "---";
Console.WriteLine(indent.Repeat(0)); //would print nothing.
Console.WriteLine(indent.Repeat(1)); //would print "---".
Console.WriteLine(indent.Repeat(2)); //would print "------".
Console.WriteLine(indent.Repeat(3)); //would print "---------".

当前回答

对于一般使用,涉及StringBuilder类的解决方案最适合重复多字符字符串。它被优化为处理大量字符串的组合,这是简单的连接所不能做到的,而且手工很难或不可能更有效地做到。这里显示的StringBuilder解决方案使用O(N)次迭代来完成,这是与重复次数成比例的固定速率。

然而,对于非常大量的重复,或者必须从它中挤出高水平的效率,更好的方法是执行类似StringBuilder的基本功能,但从目标而不是从原始字符串生成额外的副本,如下所示。

    public static string Repeat_CharArray_LogN(this string str, int times)
    {
        int limit = (int)Math.Log(times, 2);
        char[] buffer = new char[str.Length * times];
        int width = str.Length;
        Array.Copy(str.ToCharArray(), buffer, width);

        for (int index = 0; index < limit; index++)
        {
            Array.Copy(buffer, 0, buffer, width, width);
            width *= 2;
        }
        Array.Copy(buffer, 0, buffer, width, str.Length * times - width);

        return new string(buffer);
    }

这使得源/目标字符串的长度每次迭代都翻倍,从而节省了每次遍历原始字符串时重置计数器的开销,而不是顺利地读取和复制现在更长的字符串,这是现代处理器可以更有效地完成的事情。

它使用以2为底的对数来计算需要将字符串长度翻倍多少次,然后继续这样做多少次。由于要复制的剩余部分现在小于它要复制的总长度,因此它可以简单地复制它已经生成的内容的子集。

我使用了Array.Copy()方法,而不是使用StringBuilder,因为将StringBuilder的内容复制到自身中会在每次迭代中产生带有该内容的新字符串的开销。Array.Copy()避免了这种情况,同时仍然以极高的效率进行操作。

这个解决方案需要O(1 + log N)次迭代才能完成,这个速率随着重复次数的增加呈对数增长(重复次数加倍等于一次额外的迭代),这比其他方法节省了大量的时间,其他方法按比例增加。

其他回答

如果你使用。net 4.0,你可以使用字符串。与enumerable连接。重复。

int N = 5; // or whatever
Console.WriteLine(string.Concat(Enumerable.Repeat(indent, N)));

否则我会用亚当的答案。

我通常不建议使用Andrey的答案的原因很简单,ToArray()调用引入了多余的开销,而Adam建议的StringBuilder方法可以避免这些开销。也就是说,至少它不需要。net 4.0就能工作;而且它又快又简单(如果效率不是太重要的话,也不会要了你的命)。

你可以创建一个ExtensionMethod来做这件事!

public static class StringExtension
{
  public static string Repeat(this string str, int count)
  {
    string ret = "";

    for (var x = 0; x < count; x++)
    {
      ret += str;
    }

    return ret;
  }
}

或者使用@丹涛解决方案:

public static class StringExtension
{
  public static string Repeat(this string str, int count)
  {
    if (count == 0)
      return "";

    return string.Concat(Enumerable.Repeat(indent, N))
  }
}

我没有足够的代表来评论亚当的回答,但在我看来,最好的方法是这样的:

public static string RepeatString(string content, int numTimes) {
        if(!string.IsNullOrEmpty(content) && numTimes > 0) {
            StringBuilder builder = new StringBuilder(content.Length * numTimes);

            for(int i = 0; i < numTimes; i++) builder.Append(content);

            return builder.ToString();
        }

        return string.Empty;
    }

您必须检查numTimes是否大于0,否则会出现异常。

字符串和字符[版本1]

string.Join("", Enumerable.Repeat("text" , 2 ));    
//result: texttext

字符串和字符[版本2]:

String.Concat(Enumerable.Repeat("text", 2));
//result: texttext

字符串和字符[版本3]

new StringBuilder().Insert(0, "text", 2).ToString(); 
//result: texttext

识字课只有:

'5' * 3; 
//result: 555

识字课只有:

new string('5', 3);
//result: 555

扩展方法:

(工作更快-更好的WEB)

public static class RepeatExtensions
{
    public static string Repeat(this string str, int times)
    {
        var a = new StringBuilder();
        
        //Append is faster than Insert
        ( () => a.Append(str) ).RepeatAction(times) ;
        
        return a.ToString();
    }

    public static void RepeatAction(this Action action, int count)
    {
        for (int i = 0; i < count; i++)
        {
            action();
        }
    }

}

用法:

 var a = "Hello".Repeat(3); 
 //result: HelloHelloHello

没想到居然没人走老路。 我并没有对这段代码做任何声明,只是为了好玩:

public static string Repeat(this string @this, int count)
{
    var dest = new char[@this.Length * count];
    for (int i = 0; i < dest.Length; i += 1)
    {
        dest[i] = @this[i % @this.Length];
    }
    return new string(dest);
}