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;
for (int i = 0; i < lines.Length; i++)
{
TotalLength += lines[i].Length;
}
if (TotalLength > length)
{
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/>");
}
}