码迷,mamicode.com
首页 > 其他好文 > 详细

重复输出字符或字符串

时间:2017-12-29 16:16:44      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:targe   lin   复制   new   str   ram   tle   span   inf   

当你需要对某一字符或字符串重复输出时,可以参考下面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);
        }
    }
技术分享图片


测试:
技术分享图片

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

            obj.RepeatCharacter(‘$‘, 8);
            Console.WriteLine();

            obj.RepeatString("insus", 8);
            Console.WriteLine();
        }
    }
技术分享图片

重复输出字符或字符串

标签:targe   lin   复制   new   str   ram   tle   span   inf   

原文地址:https://www.cnblogs.com/cjm123/p/8144885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!