アイテムの深さに基づいて文字列の前に一定数のインデントを挿入しようとしていますが、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 "---------".
ベストアンサー1
同じ文字を繰り返すだけの場合は、文字列コンストラクタ文字とそれを繰り返す回数を受け取りますnew String(char c, int count)
。
たとえば、ダッシュを 5 回繰り返すには、次のようにします。
string result = new String('-', 5);
Output: -----