重復輸出字符或字符串
阿新 • • 發佈:2017-12-28
rac eve class 使用 技術分享 style none .com .cn
當你需要對某一字符或字符串重復輸出時,可以參考下面2個方法。一個是new 字符串,另一個是使用Linq的Enumberable的Repeat方法來實現。
class Bo { public void RepeatCharacter(char c, int times) { string output = new String(c, times); Console.WriteLine(output); } public void RepeatString(stringSource Codestr, int times) { var output = string.Concat(Enumerable.Repeat(str, times)); Console.WriteLine(output); } }
測試:
class Program { static void Main(string[] args) { Bo obj = new Bo(); obj.RepeatCharacter(Source Code‘$‘, 8); Console.WriteLine(); obj.RepeatString("insus", 8); Console.WriteLine(); } }
重復輸出字符或字符串