代码练习:简单的纯文本转HTML
2018-09-15
public static string Text2HtmlSimple(string input)
02 {
03 StringBuilder sb = new StringBuilder();
04 sb.Append("
");
05 int index = 0;
06 do
07 {
08 string toAppend = string.Empty;
09 int pos = input.IndexOf("\r\n", index);
10 if (pos == 0)
11 {
12 index = pos + 2;
13 }
14 else if (pos == input.Length - 2)
15 {
16 toAppend = input.Substring(index, pos - index);
17 if (!string.IsNullOrEmpty(toAppend))
18 {
19 sb.AppendFormat("{0}
", toAppend);20 }
21 index = pos + 2;
22 }
23 else if (pos > 0)
24 {
25 toAppend = input.Substring(index, pos - index);
26 if (!string.IsNullOrEmpty(toAppend))
27 {
28 sb.AppendFormat("{0}
", toAppend);
29 }
30 index = pos + 2;
31 }
32 else
33 {
34 toAppend = input.Substring(index, input.Length - index);
35 sb.AppendFormat("{0}
", toAppend);36 break;
37 }
38 }
39 while (index < input.Length);
40 return sb.ToString();
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。
http://www.pinlue.com/style/images/nopic.gif