Content pagination with HTML tags

Source: Internet
Author: User
Tags foreach bool count empty html tags tostring truncated

The most recent site to do static generated content must be paged so on the Internet to find n many instances are not ideal, so took a little time to write their own method

No problem at the moment (20W HTML has been generated by method)

So post the code and share it with you.

I would appreciate it if you would let me know the inadequacies or better ways.

Call ArrayList arrlt=contentpage.getpagecontent (paging content, paging size, true);

1using System;
2using System.Text;
3using System.Collections;
4using System.Text.RegularExpressions;
5
6/**////<summary>
7///Content Paging v0.1.2 gxian22@163.com
8///</summary>
9public class Contentpage
10{
One/**////<summary>
12///Content Paging
///</summary>
///<param name= "strcontent" > String content to Pagination </param>
///<param name= "intpagesize" > Paging size </param>
///<param name= "IsOpen" > last page character less than intpagesize 1/4 add to Previous page </param>
///<returns></returns>
public static ArrayList getpagecontent (string strcontent, int intpagesize, BOOL isOpen)
19 {
ArrayList arrlist = new ArrayList ();
string STRP = Strcontent;
int num = removehtml (STRP. ToString ()). length;//character length in addition to HTML tags
int bp = (intpagesize + (INTPAGESIZE/5));
24
for (int i = 0; i < (num% BP = 0)? (NUM/BP): ((NUM/BP) + 1)); i++)
26 {
Arrlist. ADD (SubString (intpagesize, ref STRP));
num = removehtml (STRP. ToString ()). Length;
if (IsOpen && num < (INTPAGESIZE/4))
30 {//less than page 1/4 Word alphanumeric to previous page
Arrlist[arrlist. Count-1] = arrlist[arrlist. COUNT-1] + STRP;
STRP = "";
33}
i = 0;
35}
The IF (STRP). Length > 0) arrlist. ADD (STRP); Greater than 1/4 characters less than intpagesize
37
return arrlist;
39}
40
/**////<summary>
///&lt; Symbol Search
///</summary>
///<param name= "CR" ></param>
///<returns></returns>
+ private static bool Isbegin (char CR)
47 {
return CR. Equals (' < ');
49}
50
Wuyi/**////<summary>
///&gt; Symbol Search
///</summary>
///<param name= "CR" ></param>
///<returns></returns>
private static bool Isend (char CR)
57 {
return CR. Equals (' > ');
59}
60
/**////<summary>
62///Intercept Paging content
///</summary>
///<param name= "index" > per page character length </param>
///<param name= "str" ></param>
///<returns></returns>
private static string SubString (int index, ref string str)
68 {
ArrayList arrlistb = new ArrayList ();
ArrayList arrliste = new ArrayList ();
Strtag string = "";
Strend char = ' 0 ';
BOOL ISBG = false;
issuendtag bool = false;
75
The index = Gindex (str, index);
+ String substr = cutstring (str, 0, index); Intercepting paging length
A string substr1 = Cutstring (str, index, str.) Length-substr. Length); Remaining characters
The IOF int = substr. LastIndexOf ("<"), Iof1 = 0;
80
81 prevent Mark Truncation #region prevent <xx > Mark truncation
82
The IF (iof > 0) iof1 = cutstring (substr, IOF, substr. LENGTH-IOF). IndexOf (">"); Whether the tag is truncated
if (Iof1 < 0)//full mark is truncated, re-intercept
85 {
index = index + SUBSTR1. IndexOf (">") + 1;
substr = cutstring (str, 0, index);
SUBSTR1 = cutstring (str, index, str.) Length-substr. Length);
89}
90
INDEXENDTB int = substr. LastIndexOf ("</tr>");
The IF (INDEXENDTB >= 0)
93 {
substr = cutstring (str, 0, INDEXENDTB);
SUBSTR1 = cutstring (str, INDEXENDTB, str.) LENGTH-INDEXENDTB);
96}
97
intsubstr int = substr. LastIndexOf ("/>") + 1;
the int intsubstr1 = substr1. IndexOf ("</");
if (intsubstr >= 0 && intsubstr1 >= 0)//<xx/> between Mark and </xx> end tag is word Furu: <a href= "# "> text text </a>
101 {
102 String substr2 = Cutstring (substr, Intsubstr, substr. LENGTH-INTSUBSTR) + cutstring (substr1, 0, INTSUBSTR1);
An if (SUBSTR2). IndexOf (' > ') = = 1 && substr2. IndexOf (' < ') = = 1)//
104 {
substr + = cutstring (substr1, 0, INTSUBSTR1);
SUBSTR2 = cutstring (substr1, INTSUBSTR1, Substr1. LENGTH-INTSUBSTR1);
an int sub2idf = SUBSTR2. IndexOf (' > ');
108 substr + = cutstring (substr2, 0, SUB2IDF);
109 SUBSTR1 = cutstring (SUBSTR2, SUB2IDF, substr2. LENGTH-SUB2IDF);
110}
111}
112 #endregion
113
114//Analysis intercept character content extraction tag
substr foreach (char cr in)
116 {
117 if (Isbegin (CR)) ISBG = true;
118 if (ISBG) Strtag = CR;
119
if (ISBG && cr. Equals ('/') && strend. Equals (' < ')) Issuendtag = true;
121
122 if (Isend (CR))
123 {
124 if (strend. Equals ('/'))//jump out of <xx/> Mark
125 {
126 ISBG = false;
127 Issuendtag = false;
128 strtag = "";
129}
130
131 if (ISBG)
132 {
The IF (! Cutstring (Strtag.tolower (), 0, 3). Equals ("<br"))
134 {
135 if (Issuendtag)
136 Arrliste.add (Strtag); End tag
137 Else
138 Arrlistb.add (Strtag); Start tag
139}
140 Issuendtag = false;
The Strtag = "";
ISBG = false;
143}
144}
145 strend = CR;
146}
147
148//Find not closed mark
149 for (int b = 0; b < arrlistb.count; b++)
150 {
151 for (int e = 0; e < arrliste.count; e++)
152 {
153 string strb = Arrlistb[b]. ToString (). ToLower ();
154 int num = strb. IndexOf (");
(num > 0) strb = cutstring (strb, 0, num) + ">";
156 if (STRB. ToLower (). Replace ("<", "</"). Equals (Arrliste[e]. ToString (). ToLower ()))
157 {
158 arrlistb.removeat (b);
159 Arrliste.removeat (e);
160 B =-1;
161 break;
162}
163}
164}
165
166//Close truncated mark
167 for (int i = Arrlistb.count i > 0; i--)
168 {
169 string stral = Arrlistb[i-1]. ToString ();
170 substr + = (stral. IndexOf ("") = = 1? Stral. Replace (">", "/>"): cutstring (stral, 0, Stral. IndexOf ("")) + "/>");
171}
172//Full top page truncation label
173 String strtag = "";
174 for (int i = 0; i < arrlistb.count i++) Strtag + = Arrlistb[i]. ToString ();
175
176 str = strtag + substr1; Change the original string
177 return substr; Return intercept content
178}
179
180/**////<summary>
181///return True character length
///</summary>
183///<param name= "str" ></param>
184///<param name= "index" ></param>
185///<returns></returns>
186 private static int gindex (string str, int index)
187 {
188 bool ISBG = false;
189 bool Issuendtag = FALSE;
190 bool isnbsp = False, isrnbsp = false;;
191 String strnbsp = "";
The 0, c = 0;
193 foreach (char cr in str)
194 {
195 if (!ISBG && Isbegin (CR)) {ISBG = true; Issuendtag = false;}
196 if (ISBG && isend (CR)) {ISBG = false; Issuendtag = true;}
197 if (Issuendtag &&!ISBG)
198 {//Not in HTML tags
199 if (CR. Equals (' & ')) isnbsp = true;
if (isnbsp)
201 {
STRNBSP + CR. ToString ();
203 if (strnbsp. Length > 6) {isnbsp = false; strnbsp = "";}
204 if (CR. Equals (';')) ISNBSP = false;//
205}
206 if (!isnbsp &&! "". Equals (strnbsp)) isrnbsp = strnbsp. ToLower (). Equals ("&nbsp;");
207}
208
209 if (Issuendtag &&!CR. Equals (' \ n ') &&!CR. Equals (' R ') &&!CR. Equals (' \ R ')) C + +;
210 if (isrnbsp) {c = c-6; isrnbsp = false; strnbsp = ""; }
211
212 i++;
213
214 if (c = = index) return i;
215}
216 return i;
217}
218
219/**////<summary>
220///Remove HTML tags
221///</summary>
222///<param name= "content" ></param>
223///<returns></returns>
224 public static string removehtml (string content)
225 {
Content = Regex.Replace (content, @ "<[^>]*>", String. Empty, regexoptions.ignorecase);
Regex.Replace return (content, "&nbsp;", String. Empty, regexoptions.ignorecase);
228}
229
230/**////<summary>
231///The substring of the specified length from the specified position of the string
232///</summary>
233///<param name= "str" > Original string </param>
234///<param name= "StartIndex" > substring start position </param>
235///<param name= "Length" > substring lengths </param>
236///<returns> substring </returns>
237 public static string cutstring (string str, int startIndex, int length)
238 {
239 if (startIndex >= 0)
240 {
241 if (length < 0)
242 {
243 length = length *-1;
244 if (Startindex-length < 0)
245 {
246 length = StartIndex;
247 StartIndex = 0;
248}
249 Else
250 {
251 StartIndex = startindex-length;
252}
253}
254
255 if (StartIndex > str. Length) return "";
256
257}
258 Else
259 {
The if (length < 0)
261 {
262 return "";
263}
264 Else
265 {
266 if (length + startIndex > 0)
267 {
268 length = length + StartIndex;
MB StartIndex = 0;
270}
271 Else
272 {
273 return "";
274}
275}
276}
277
278 if (str. Length-startindex < length) length = str. Length-startindex;
279 try
280 {
281 return str. Substring (startIndex, length);
282}
283 Catch
284 {
285 return str;
286}
287}
288}
289

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.