/// <Summary>
/// Truncates a string by byte length (HTML is supported)CodeStyle string)
/// </Summary>
/// <Param name = "Param"> String parameters to be truncated </Param>
/// <Param name = "length"> Length of truncated bytes </Param>
/// <Param name = "end"> String supplemented at the end of the string </Param>
/// <Returns> Returns the truncated string. </Returns>
Public Static String Stringhtml ( 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 ; // Is it 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 );
// Extract the HTML tag from the truncated string.
String Temp_result = RegEx. Replace (result. tostring (), " (>) [^ <>] * (<?) " , " $1 $2 " , Regexoptions. ignorecase );
// Remove HTML tags that do not need to be collocated.
Temp_result = RegEx. Replace (temp_result, @" </? (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) [^ <>] */?> "
, "" , Regexoptions. ignorecase );
// Remove the paired HTML Tag
Temp_result = RegEx. Replace (temp_result, @" <([A-Za-Z] +) [^ <>] *> (.*?) </\ 1> " , "" , Regexoptions. ignorecase );
// 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 ();
}
Original: http://blog.163.com/chimingchong/blog/static/8186599200882333056222/