工资不涨,物价在疯狂的涨!

博客园 首页 新随笔 联系 订阅 管理
 

方法一:

        ///<summary>

        ///字符串重复N

        ///</summary>

        ///<param name="str"></param>

        ///<param name="n"></param>

        ///<returns></returns>

        ///<example>

        ///<![CDATA[Console.WriteLine(RepeatString("H", 5));]]>

        ///</example>

        public static string RepeatString(string str, int n)

        {

            char[] arr = str.ToCharArray();

            char[] arrDest = new char[arr.Length * n];

            for (int i = 0; i < n; i++)

            {

                Buffer.BlockCopy(arr, 0, arrDest, i * arr.Length * 2, arr.Length * 2);

            }

            return new string(arrDest);

        }

方法二:

        ///<summary>

        ///使用范型,重复N

        ///</summary>

        ///<param name="str">泛型参数,根据传入的参数判断是什么类型</param>

        ///<param name="n"></param>

        ///<returns></returns>

        ///<example>

        ///<![CDATA[

        /// Console.WriteLine(RepeatStringByGeneric(54, 10));

        /// Console.WriteLine(RepeatStringByGeneric("abc", 10));

        /// ]]>

        ///</example>

        public static string RepeatStringByGeneric<T>(T str, int n)//泛型方法

        {

            string s = str.ToString();

            char[] arr = s.ToCharArray();

            char[] arrDest = new char[arr.Length * n];

            for (int i = 0; i < n; i++)

            {

                Buffer.BlockCopy(arr, 0, arrDest, i * arr.Length * 2, arr.Length * 2);

            }

            return new string(arrDest);

        }

方法三:(本方法只适用于重复单一字符)

            string str = new string('A',100);//注意:new string实例化的是char类型,100是长度

            Console.WriteLine(str);

测试环境:VS2008 ENU

posted on 2008-02-01 15:26  腾云驾雾  阅读(273)  评论(0编辑  收藏  举报