/// <Summary>
/// Truncate a string by byte length (supports truncate a string with HTML code style)
/// </Summary>
/// <Param name = "param"> string parameter to be truncated </param>
/// <Param name = "length"> truncated bytes </param>
/// <Param name = "end"> string added at the end of the string </param>
/// <Returns> returns the truncated 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)
{
N = n + 1;
// UNICODE characters in two bytes
If (System. Text. Encoding. Default. GetBytes (temp + ""). Length> 1)
{
N = n + 1;
}
}
Result. Append (temp );
If (n> = length)
{
Break;
}
}
Result. Append (end );
// Retrieve the HTML tag from the truncated string
String temp_result = result. ToString (). Replace ("(>) [^ <>] * (<?)", "$1 $2 ″);
// Remove the HTML Tag that does not need the element tag
Temp_result = temp_result.Replace (@ "</? (AREA | BASE | BASEFONT | BODY | BR | COL | COLGROUP | DD | DT | FRAME | HEAD | HR | HTML
| IMG | INPUT | ISINDEX | LI | LINK | META | OPTION | P | PARAM | TBODY | TD | TFOOT | TH | THEAD
| TR | area | base | basefont | body | br | col | colgroup | dd | dt | frame | head | hr | html | img | input | isindex | li | link | meta
| Option | p | param | tbody | td | tfoot | th | thead | tr) [^ <>] */?> ",
"");
// Remove the paired HTML Tag
Temp_result = temp_result.Replace (@ "<([a-zA-Z] +) [^ <>] *> (.*?) </1> "," $2 ″);
// Use a regular expression to retrieve a tag
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 ();
}