• 代码练习:简单的纯文本转HTML


    谁能写一个函数,实现如下功能,不需要调试一次就通过,且没有BUG。需求:输入:"ab\r\ncde\r\nfghi"输出:"<p>ab</p><p>cde</p><p>fghi</p>",注意无论任何输出<p>和</p>都要配对出现,且<p>和</p>之间不能为空

    public static string Text2HtmlSimple(string input)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<p>");
        int index = 0;
        do
        {
            string toAppend = string.Empty;
            int pos = input.IndexOf("\r\n", index);
            if (pos == 0)
            {
                index = pos + 2;
            }
            else if (pos == input.Length - 2)
            {
                toAppend = input.Substring(index, pos - index);
                if (!string.IsNullOrEmpty(toAppend))
                {
                    sb.AppendFormat("{0}</p>", toAppend);
                }
                index = pos + 2;
            }
            else if (pos > 0)
            {
                toAppend = input.Substring(index, pos - index);
                if (!string.IsNullOrEmpty(toAppend))
                {
                    sb.AppendFormat("{0}</p><p>", toAppend);
                }
                index = pos + 2;
            }
            else
            {
                toAppend = input.Substring(index, input.Length - index);
                sb.AppendFormat("{0}</p>", toAppend);
                break;
            }
        }
        while (index < input.Length);
        return sb.ToString();
    }
    

  • 相关阅读:
    java_监控工具jvisualvm
    bzoj3667: Rabin-Miller算法
    bzoj3677: [Apio2014]连珠线
    4070: [Apio2015]雅加达的摩天楼
    4069: [Apio2015]巴厘岛的雕塑
    4071: [Apio2015]巴邻旁之桥
    bzoj2653: middle
    1500: [NOI2005]维修数列
    bzoj4262: Sum
    bzoj4540: [Hnoi2016]序列
  • 原文地址:https://www.cnblogs.com/onlytiancai/p/1847280.html
Copyright © 2020-2023  润新知