) Truncate strings by byte length (HTML code-style strings can be captured)

Source: Internet
Author: User
///   <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/

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.