我试图在基于项深度的字符串之前插入一定数量的缩进,我想知道是否有一种方法可以返回一个重复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 "---------".

当前回答

public static class StringExtensions
{
    public static string Repeat(this string input, int count)
    {
        if (string.IsNullOrEmpty(input) || count <= 1)
            return input;

        var builder = new StringBuilder(input.Length * count);

        for(var i = 0; i < count; i++) builder.Append(input);

        return builder.ToString();
    }
}

其他回答

对于一般使用,涉及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)次迭代才能完成,这个速率随着重复次数的增加呈对数增长(重复次数加倍等于一次额外的迭代),这比其他方法节省了大量的时间,其他方法按比例增加。

另一种方法是将string视为IEnumerable<char>,并使用通用扩展方法将集合中的项乘以指定的因子。

public static IEnumerable<T> Repeat<T>(this IEnumerable<T> source, int times)
{
    source = source.ToArray();
    return Enumerable.Range(0, times).SelectMany(_ => source);
}

在你的例子中:

string indent = "---";
var f = string.Concat(indent.Repeat(0)); //.NET 4 required
//or
var g = new string(indent.Repeat(5).ToArray());

如果你只想重复相同的字符,你可以使用string构造函数接受一个字符和重复次数new string (char c, int count)。

例如,重复五次破折号:

string result = new String('-', 5);
Output: -----

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

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;      

最高性能的字符串解决方案

string result = new StringBuilder().Insert(0, "---", 5).ToString();