Asp.net (the first few lines of the document are taken as the list summary) and the HTML code is returned without loss.

Source: Internet
Author: User

Copy codeThe Code is as follows: <BR>/* <STRONG> it has been a long time. I copied the code. I remember that some exceptions should be handled when using it, most of them are OK. </STRONG>
* Function:
* Description: lossless HTML code returned
*
* Author: Kas (QQ: 10590916)
* Modification: 2010-12-13,
*/

Public static string LeftH (string str, int l)
{
// To prevent errors
String odstr = str;
Bool isHtml = false; // determines whether the start of the truncation is a string.
Int maxLen = str. Length;
Int n = 0, I = 0, B = 0, e = 0;
String c, c2 = "", Start = "", EndLabel = "", AllLabel = "";
If (l> = str. Length) return str; // The truncated field is longer than the character Length.

Bool isErr = false;
Try
{
While (n <l & I <maxLen)
{
// 1
I ++;
C = str. Substring (I-1, 1 );

String end2str = "";
Try
{
End2str = str. Substring (I-1, 2 );
}
Catch
{
End2str = "";
}
If (c = "<" & end2str! = "</")
{
IsHtml = true;
B = I; // record a location
// 5 resolve unpaired tags

String end2tag = "";
Try
{
End2tag = str. Substring (I, 2 );
}
Catch
{
End2tag = "";
}

If (end2tag = "br ")
{
IsHtml = false;
}
If (str. Substring (I, 1). ToLower () = "% ")
{
IsHtml = false;
}
If (str. Substring (I, 1). ToLower () = "? ")
{
IsHtml = false;
}
Else if (end2tag = "hr ")
{
IsHtml = false;
}
// 5end
}
// 1end

// 2
If (c = "<" & end2str = "</")
{
C2 = str. Substring (I-1, str. Substring (I-1). IndexOf (">") + 1 );
AllLabel = AllLabel. Substring (c2.Length );
}
// 2end

Start = Start + c; // record the current character and its prefix

If (! IsHtml)
{
// 6 accurately count
Try
{
If (str. Substring (I, 2). ToLower () = "br ")
{
}
Else if (str. Substring (I, 2). ToLower () = "hr ")
{
}
Else
{
N ++;
}
}
Catch
{
// If an error occurs, plain text is returned (@ "<[^>] + >|</[^>] +> ");
// Odstr = Regex. Replace (odstr, @ "<[^>] + >|</[^>] +>", "", RegexOptions. IgnoreCase );
Odstr = odstr. Replace ("<", "<"). Replace (">", "> ");
If (l> odstr. Length) l = odstr. Length;
Return odstr. Substring (0, l );
}
// 6end
}
Else
{
If (c = ">") // if the loop is>, the intermediate part of the paired tag to be processed
{
If (isHtml)
{
EndLabel = str. Substring (B, I-B); // obtain the code between <to>
E = EndLabel. IndexOf (""); // tag attributes are separated by spaces to obtain the tag name.
If (e> 0)
{
EndLabel = "</" + EndLabel. Substring (0, e) + "> ";
}
Else
{
EndLabel = "</" + EndLabel;
}
AllLabel = EndLabel + AllLabel; // record the current end label and all related end labels
}
IsHtml = false; // reset the attribute to start from
}
}
// 4. When an unclosed tag is found at the end, the tag is retried.
If (n + 1> = l)
{
If (Limit N (Start, "<")! = Nth N (Start, "> "))
{
N --;
}
}
// 4end
} // End while

}
Catch
{
IsErr = true;
}

String newReString = Start + AllLabel + "...";

// Verify again
Regex Rg = new Regex ("<. [^>/] +>", RegexOptions. Compiled );
Regex Rg2 = new Regex (@ "<\ s */[a-z] \ s *>", RegexOptions. Compiled );

If (FIG (newReString, "<")! = Jsonlen (newReString, "> "))
{
IsErr = true;
}
Else if (FIG (newReString. Replace ("," \ ""), "\" ") % 2 = 1)
{
IsErr = true;
}
Else if (Rg. Matches (newReString). Count! = Rg2.Matches (newReString). Count)
{
IsErr = true;
}
If (isErr)
{
Odstr = odstr. replace ("<", "<"). replace (">", "> "). replace ("\"","""). replace ("'","'");
If (l> odstr. Length)
{NewReString = odstr ;}
Else
{
Try
{
NewReString = odstr. Substring (0, l) + "...";
}
Catch
{
NewReString = odstr;
}
}
}

Return newReString; // return
}

Related Article

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.