Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Text. regularexpressions;
Using system. text;
Public partial class test system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = pspan style = 'font-size larger 'Five span style = 'color red' Dalian spanspan style = 'font-family 'pool span wind span style = 'font-family youyuan 'scenic span official website spanspan style = 'font-family 'span style = ''span style = 'font-size larger' Song spanspanspanspan style = 'font-family _ gb2312 'span style = 'font-size larger 'spanspanp;
Response. Write (substringtohtml (STR, 16 ,...));
}
Summary
Truncates a string by byte length (HTML is supported) Code Style string)
Summary
Param name = the string parameter Param to be intercepted by Param
Param name = Length: the length of the truncated bytes. Param
Param name = end: the string Param added at the end of the string
Returns returns the intercepted string returns
Public String substringtohtml (string Param, int length, string end)
{
String Pattern = NULL;
Matchcollection M = NULL;
Stringbuilder result = new stringbuilder ();
Int n = 0;
Char temp;
Bool iscode = false; is it HTML code?
Bool ishtml = false; whether it is a special HTML character, such as & nbsp;
Char [] pchar = Param. tochararray ();
For (INT I = 0; I pchar. length; I ++)
{
Temp = pchar [I];
If (temp = '')
{
Iscode = true;
}
Else if (temp = '&')
{
Ishtml = true;
}
Else if (temp = ''& iscode)
{
N = n-1;
Iscode = false;
}
Else if (temp = ';' & ishtml)
{
Ishtml = false;
}
If (! Iscode &&! Ishtml)
{< br> N = n + 1;
Unicode characters occupy two bytes
If (system. text. encoding. default. getbytes (temp + ). length 1)
{< br> N = n + 1;
}< BR >}
Result. append (temp );
If (n = length)
{
Break;
}
}
Result. append (end );
Extract the HTML tag from the truncated string.
String temp_result = result. tostring (). Replace () [^] (), $1 $2 );
Remove HTML tags that do not need to be collocated.
Temp_result = temp_result.replace (@ (kerberodybrcolgroupdddtframeheadhrhtmlimginputisindexlilinkmetaoptionpparamtbodytdtfootththeadtr) [^],
);
Remove the paired HTML Tag
Temp_result = temp_result.replace (@ ([A-Za-Z] +) [^] (.) 1, $2 );
Use regular expressions to retrieve tags
Pattern = ([A-Za-Z] +) [^]);
M = RegEx. Matches (temp_result, pattern );
Arraylist endhtml = new arraylist ();
Foreach (match MT in m)
{
Endhtml. Add (mt. Result ($1 ));
}
Incomplete HTML tags
For (INT I = endhtml. Count-1; I = 0; I --)
{
Result. append ();
Result. append (endhtml [I]);
Result. append ();
}
Return result. tostring ();
}
}