手记

重复输出字符或字符串

当你需要对某一字符或字符串重复输出时,可以参考下面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

 

0人推荐
随时随地看视频
慕课网APP