1. 程式人生 > >重復輸出字符或字符串

重復輸出字符或字符串

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(string
str, int times) { var output = string.Concat(Enumerable.Repeat(str, times)); Console.WriteLine(output); } }
Source Code


測試:
技術分享圖片

技術分享圖片
 class Program
    {
        static void Main(string[] args)
        {
            Bo obj = new Bo();

            obj.RepeatCharacter(
$, 8); Console.WriteLine(); obj.RepeatString("insus", 8); Console.WriteLine(); } }
Source Code

重復輸出字符或字符串