Codewars 7 Kyu : Thinkful – String Drills: Repeater

题目

Write a function named repeater() that takes two arguments (a string and a number), and returns a new string where the input string is repeated that many times.

Example:

Repeater.repeat("a", 5)

should return

"aaaaa"

代码

using System.Text;
public class Kata
{
  public static string Repeater(string s, int n)
  {
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<n;i++){
      sb.Append(s);
    }
    string output=sb.ToString();
    return output;//Your code goes here.
  }
}

参考资料

本题考虑用StringBuilder做,StringBuilder使用Append方法时,第一个参数只能是char,第二个参数为添加次数
https://docs.microsoft.com/zh-cn/dotnet/api/system.text.stringbuilder.-ctor?view=netframework-4.7.2

发表评论

您的电子邮箱地址不会被公开。