我试图在基于项深度的字符串之前插入一定数量的缩进,我想知道是否有一种方法可以返回一个重复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)次迭代才能完成,这个速率随着重复次数的增加呈对数增长(重复次数加倍等于一次额外的迭代),这比其他方法节省了大量的时间,其他方法按比例增加。

其他回答

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

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

我没有看到这个解。我发现在我目前的软件开发工作中,它更简单:

public static void PrintFigure(int shapeSize)
{
    string figure = "\\/";
    for (int loopTwo = 1; loopTwo <= shapeSize - 1; loopTwo++)
    {
        Console.Write($"{figure}");
    }
}

我喜欢你给出的答案。我过去也用过同样的方法:

"".PadLeft(3*Indent,'-')

这将实现创建缩进,但技术上的问题是重复一个字符串。如果字符串缩进是像>-<这样的东西,那么这个和接受的答案一样将不起作用。在这种情况下,c0rd使用StringBuilder的解决方案看起来不错,尽管StringBuilder的开销实际上可能不是最高性能的。一种选择是构建一个字符串数组,用缩进字符串填充它,然后连接它。一点点:

int Indent = 2;
        
string[] sarray = new string[6];  //assuming max of 6 levels of indent, 0 based

for (int iter = 0; iter < 6; iter++)
{
    //using c0rd's stringbuilder concept, insert ABC as the indent characters to demonstrate any string can be used
    sarray[iter] = new StringBuilder().Insert(0, "ABC", iter).ToString();
}

Console.WriteLine(sarray[Indent] +"blah");  //now pretend to output some indented line

我们都喜欢聪明的解决方案,但有时简单是最好的。

不知道这将如何执行,但这是一段简单的代码。(我可能让它看起来比实际更复杂。)

int indentCount = 3;
string indent = "---";
string stringToBeIndented = "Blah";
// Need dummy char NOT in stringToBeIndented - vertical tab, anyone?
char dummy = '\v';
stringToBeIndented.PadLeft(stringToBeIndented.Length + indentCount, dummy).Replace(dummy.ToString(), indent);

或者,如果您知道期望的最大层数,则可以声明一个数组并在其中建立索引。你可能想让这个数组是静态的或者是常量。

string[] indents = new string[4] { "", indent, indent.Replace("-", "--"), indent.Replace("-", "---"), indent.Replace("-", "----") };
output = indents[indentCount] + stringToBeIndented;      

字符串和字符[版本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