首页 > 其他 > 详细

重复输出字符或字符串

时间:2017-12-28 19:07:53      阅读:216      评论:0      收藏:0      [点我收藏+]

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

 

重复输出字符或字符串

原文:https://www.cnblogs.com/insus/p/8137026.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!