最近網站要做靜態生成內容必須分頁所以在網上找了N多實例都不理想,所以花了點時間自己寫了個方法
目前來說沒發現什麼問題(已用方法生成20W個html)
所以把代碼貼出來與大家分享。
不足之處或有更好的方法請大家告知我不勝感激。
調用ArrayList arrlt=ContentPage.GetPageContent("分頁內容", 分頁大小,true);
1using System;
2using System.Text;
3using System.Collections;
4using System.Text.RegularExpressions;
5
6 /**//// <summary>
7 /// 內容分頁 v0.1.2 [email protected]
8 /// </summary>
9public class ContentPage
10{
11 /**//// <summary>
12 /// 內容分頁
13 /// </summary>
14 /// <param name="strContent">要分頁的字符串內容</param>
15 /// <param name="intPageSize">分頁大小</param>
16 /// <param name="isOpen">最後一頁字符小於intPageSize的1/4加到上一頁</param>
17 /// <returns></returns>
18 public static ArrayList GetPageContent(string strContent, int intPageSize, bool isOpen)
19 {
20 ArrayList arrlist = new ArrayList();
21 string strp = strContent;
22 int num = RemoveHtml(strp.ToString()).Length;//除html標記後的字符長度
23 int bp = (intPageSize + (intPageSize / 5));
24
25 for (int i = 0; i < ((num % bp == 0) ? (num / bp) : ((num / bp) + 1)); i++)
26 {
27 arrlist.Add(SubString(intPageSize, ref strp));
28 num = RemoveHtml(strp.ToString()).Length;
29 if (isOpen && num < (intPageSize / 4))
30 { // 小於分頁1/4字符加到上一頁
31 arrlist[arrlist.Count - 1] = arrlist[arrlist.Count - 1] + strp;
32 strp = "";
33 }
34 i = 0;
35 }
36 if (strp.Length > 0) arrlist.Add(strp); //大於1/4字符 小於intPageSize
37
38 return arrlist;
39 }
40
41 /**//// <summary>
42 /// < 符號搜索
43 /// </summary>
44 /// <param name="cr"></param>
45 /// <returns></returns>
46 private static bool IsBegin(char cr)
47 {
48 return cr.Equals('<');
49 }
50
51 /**//// <summary>
52 /// > 符號搜索
53 /// </summary>
54 /// <param name="cr"></param>
55 /// <returns></returns>
56 private static bool IsEnd(char cr)
57 {
58 return cr.Equals('>');
59 }
60
61 /**//// <summary>
62 /// 截取分頁內容
63 /// </summary>
64 /// <param name="index">每頁字符長度</param>
65 /// <param name="str"></param>
66 /// <returns></returns>
67 private static string SubString(int index, ref string str)
68 {
69 ArrayList arrlistB = new ArrayList();
70 ArrayList arrlistE = new ArrayList();
71 string strTag = "";
72 char strend = '0';
73 bool isBg = false;
74 bool IsSuEndTag = false;
75
76 index = Gindex(str, index);
77 string substr = CutString(str, 0, index); //截取分頁長度
78 string substr1 = CutString(str, index, str.Length - substr.Length); //剩余字符
79 int iof = substr.LastIndexOf("<"), iof1 = 0;
80
81 防止標記截斷#region 防止<xx >標記截斷
82
83 if (iof > 0) iof1 = CutString(substr, iof, substr.Length - iof).IndexOf(">"); // 標記是否被截斷
84 if (iof1 < 0) //完整標記被截斷,則重新截取
85 {
86 index = index + substr1.IndexOf(">") + 1;
87 substr = CutString(str, 0, index);
88 substr1 = CutString(str, index, str.Length - substr.Length);
89 }
90
91 int indexendtb = substr.LastIndexOf("</tr>");
92 if (indexendtb >= 0)
93 {
94 substr = CutString(str, 0, indexendtb);
95 substr1 = CutString(str, indexendtb, str.Length - indexendtb);
96 }
97
98 int intsubstr = substr.LastIndexOf("/>") + 1;
99 int intsubstr1 = substr1.IndexOf("</");
100 if (intsubstr >= 0 && intsubstr1 >= 0) // <xx /> 標記與 </xx>結束標記間是否字符 如:<a href="#"><img src="abc.jpg" />文字文字文字文字</a>
101 {
102 string substr2 = CutString(substr, intsubstr, substr.Length - intsubstr) + CutString(substr1, 0, intsubstr1);
103 if (substr2.IndexOf('>') == -1 && substr2.IndexOf('<') == -1) //
104 {
105 substr += CutString(substr1, 0, intsubstr1);
106 substr2 = CutString(substr1, intsubstr1, substr1.Length - intsubstr1);
107 int sub2idf = substr2.IndexOf('>');
108 substr += CutString(substr2, 0, sub2idf);
109 substr1 = CutString(substr2, sub2idf, substr2.Length - sub2idf);
110 }
111 }
112 #endregion
113
114 //分析截取字符內容提取標記
115 foreach (char cr in substr)
116 {
117 if (IsBegin(cr)) isBg = true;
118 if (isBg) strTag += cr;
119
120 if (isBg && cr.Equals('/') && strend.Equals('<')) IsSuEndTag = true;
121
122 if (IsEnd(cr))
123 {
124 if (strend.Equals('/')) //跳出 <XX />標記
125 {
126 isBg = false;
127 IsSuEndTag = false;
128 strTag = "";
129 }
130
131 if (isBg)
132 {
133 if (!CutString(strTag.ToLower(), 0, 3).Equals("<br"))
134 {
135 if (IsSuEn