Col*_*ett 4 c# string indentation
可能重复:
我可以"乘"一个字符串(在C#中)吗?
在Python中我可以这样做:
>>> i = 3
>>> 'hello' * i
'hellohellohello'
Run Code Online (Sandbox Code Playgroud)
我怎样才能在C#中使用Python中的字符串? 我可以轻松地在for循环中执行它,但这会变得乏味且无表情.
最终我正在递归地写出控制台,每次调用都会递增缩进级别.
parent
child
child
child
grandchild
Run Code Online (Sandbox Code Playgroud)
这是最简单的事情"\t" * indent.
Sha*_*mer 19
在这篇文章中有一个扩展方法.
public static string Multiply(this string source, int multiplier)
{
StringBuilder sb = new StringBuilder(multiplier * source.Length);
for (int i = 0; i < multiplier; i++)
{
sb.Append(source);
}
return sb.ToString();
}
string s = "</li></ul>".Multiply(10);
Run Code Online (Sandbox Code Playgroud)
Chr*_*tal 12
如果你只需要一个角色就可以:
new string('\t', i)
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此帖子.
Nol*_*rin 11
BCL没有内置任何内容,但是一些LINQ可以很容易地完成任务:
var multiplied = string.Join("", Enumerable.Repeat("hello", 5).ToArray());
Run Code Online (Sandbox Code Playgroud)
Zac*_*ary 11
我是这样做的......
string value = new string(' ',5).Replace(" ","Apple");
Run Code Online (Sandbox Code Playgroud)