字符串换行(每行的长度,多于这个长度自动换行)


static void Main(string[] args)
        {

            string s = "Hello_C#_World!##";
            string newS= CutStr(s, 5);
            Console.Write(newS);
            Console.Read();
        }

        /// <summary>
        ///  截取字符串
        /// </summary>
        /// <param name="str">待截取的字符串</param>
        /// <param name="len">每行的长度,多于这个长度自动换行</param>
        /// <returns></returns>
        public static string CutStr(string str, int len)
        {
            string s = "";

            for (int i = 0; i <= str.Length; i++)
            {
                int r = i % len;
                int last = (str.Length/ len) * len;
                if (i != 0 && i <= last)
                {
                    if (r == 0)
                    {
                        s += str.Substring(i - len, len) + "\r\n";
                    }
                    
                }
                else if (i>last)
                {
                    s += str.Substring(i-1);
                    break;
                }
            }
            return s;
        }

  

 
posted @ 2012-10-25 22:18  威少  阅读(837)  评论(0编辑  收藏  举报