我试图在一个基于项深度的字符串之前插入一定数量的缩进,我想知道是否有一种方法可以重复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 "---------".
Run Code Online (Sandbox Code Playgroud)
Ahm*_*eed 501
如果您只想重复相同的字符,可以使用接受char 的字符串构造函数和重复它的次数new String(char c, int count)
.
例如,要重复五次破折号:
string result = new String('-', 5); // -----
Run Code Online (Sandbox Code Playgroud)
Dan*_*Tao 252
如果您使用的是.NET 4.0,则可以string.Concat
与它一起使用Enumerable.Repeat
.
int N = 5; // or whatever
Console.WriteLine(string.Concat(Enumerable.Repeat(indent, N)));
Run Code Online (Sandbox Code Playgroud)
否则我会选择亚当的答案.
我通常不会建议使用Andrey的答案的原因很简单,这个ToArray()
调用引入了多余的开销,而这种开销可以StringBuilder
通过Adam建议的方法来避免.也就是说,至少它可以在不需要.NET 4.0的情况下工作; 它既快速又简单(如果效率不是太大的话,也不会杀了你).
Ada*_*son 45
public static class StringExtensions
{
public static string Repeat(this string input, int count)
{
if (!string.IsNullOrEmpty(input))
{
StringBuilder builder = new StringBuilder(input.Length * count);
for(int i = 0; i < count; i++) builder.Append(input);
return builder.ToString();
}
return string.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
c0r*_*0rd 41
字符串的最佳性能解决方案
string result = new StringBuilder().Insert(0, "---", 5).ToString();
Run Code Online (Sandbox Code Playgroud)
Bob*_*ers 20
对于许多场景,这可能是最好的解决方案:
public static class StringExtensions
{
public static string Repeat(this string s, int n)
=> new StringBuilder(s.Length * n).Insert(0, s, n).ToString();
}
Run Code Online (Sandbox Code Playgroud)
用法是:
text = "Hello World! ".Repeat(5);
Run Code Online (Sandbox Code Playgroud)
这建立在其他答案的基础上(特别是@ c0rd).除了简单性之外,它还具有以下功能,而不是所讨论的所有其他技术共享:
StringBuilder
通过存储预分配有效使用.Ste*_*end 19
如果所需的字符串只包含一个char,请使用String.PadLeft.
public static string Indent(int count, char pad)
{
return String.Empty.PadLeft(count, pad);
}
Run Code Online (Sandbox Code Playgroud)
信用到期了
LiR*_*RoN 15
你可以重复你的字符串(如果它不是一个字符)并连接结果,如下所示:
String.Concat(Enumerable.Repeat("---", 5))
Run Code Online (Sandbox Code Playgroud)
Tho*_*que 11
我会选择Dan Tao的答案,但是如果你不使用.NET 4.0,你可以这样做:
public static string Repeat(this string str, int count)
{
return Enumerable.Repeat(str, count)
.Aggregate(
new StringBuilder(),
(sb, s) => sb.Append(s))
.ToString();
}
Run Code Online (Sandbox Code Playgroud)
字符串和字符 [版本 1]
string.Join("", Enumerable.Repeat("text" , 2 ));
//result: texttext
Run Code Online (Sandbox Code Playgroud)
字符串 [版本 2]:
String.Concat(Enumerable.Repeat("text", 2));
//result: texttext
Run Code Online (Sandbox Code Playgroud)
字符串和字符 [版本 3]
new StringBuilder().Insert(0, "text", 2).ToString();
//result: texttext
Run Code Online (Sandbox Code Playgroud)
仅字符:
'5' * 3;
//result: 555
Run Code Online (Sandbox Code Playgroud)
(工作更快 - 更适合 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var a = "Hello".Repeat(3);
//result: HelloHelloHello
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
174073 次 |
最近记录: |