且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在转发器中绑定多行文本

更新时间:2023-12-04 12:39:28

I have considered 'Desc' as string input




string Desc = "Hell\r\noWorld\r\nHow\r\nAre\r\n";
        protected string Limit(int length)
        {
            string strDesc = Convert.ToString(Desc);
            string[] lines = Regex.Split(strDesc, Environment.NewLine);
            string s = "";
            int TotalLength = 0;
            int cnt = 0;
            //Finding total length of all strings
            for (int i = 0; i < lines.Length; i++)
            {
                TotalLength += lines[i].Length;
            }
            if (TotalLength > length)
            {
                //Making of String with "<br/>
                s = lines[0];
                length = length - lines[0].Length;
                cnt = 1;
                while (length > 0)
                {
                    if (lines[cnt].Length < length)
                    {
                        s += "<br/>" + lines[cnt];
                        length = length - lines[cnt].Length;
                    }
                    else
                    {
                        s += "<br/>" + lines[cnt].Substring(0, length);
                        length = 0;
                    }
                    cnt++;
                }
                return s + "...";
            }
            else
            {
                return strDesc.Replace(Environment.NewLine, "<br/>");
            }

        }