In C#, there is no such operator as in per or Python where you can easily repeat a string n times. But no worry, there is way that you can easily tot make the String to have thei function to repeat the string n times.
here is the code
public static class StringExtensions
{
public static string Repeat(this char charToRepeat, int repeat)
{
return new string(charToRepeat, repeat);
}
public static string Repeat(this string stringToRepeat, int repeat)
{
var builder = new StringBuilder(repeat * stringToRepeat.Length);
for (int i = 0; i < repeat; i++)
{
builder.Append(stringToRepeat);
}
return builder.ToString();
}
}
本文介绍了一种在C#中实现字符串重复的方法,通过定义扩展方法使字符串能够被重复指定次数,提高了代码的简洁性和可读性。
3385

被折叠的 条评论
为什么被折叠?



