Simple text to HTML conversion

前端 未结 6 767
醉梦人生
醉梦人生 2021-01-04 07:33

I have a very simple asp:textbox with the multiline attribute enabled. I then accept just text, with no markup, from the textbox. Is there a comm

6条回答
  •  死守一世寂寞
    2021-01-04 08:07

    Combining all previous plus considering titles and subtitles within the text comes up with this:

    public static string ToHtml(this string text)
    {
        var sb = new StringBuilder();
    
        var sr = new StringReader(text);
        var str = sr.ReadLine();
        while (str != null)
        {
            str = str.TrimEnd();
            str.Replace("  ", "  ");
            if (str.Length > 80)
            {
                sb.AppendLine($"

    {str}

    "); } else if (str.Length > 0) { sb.AppendLine($"{str}
    "); } str = sr.ReadLine(); } return sb.ToString(); }

    the snippet could be enhanced by defining rules for short strings

提交回复
热议问题