asp.net secure intercept the specified length of HTML or UBB string _ Practical Tips

Source: Internet
Author: User
Tags truncated
When you intercept a string, you need to keep track of whether each tag is closed, and if you intercept a specified length with a label that has not been closed, then we need to close the tag or remove the tab that is not closed. Regardless of the need to close the label, the HTML start and end tags always appear in pairs, and we can iterate through the input string and put it in the stack at the beginning of the tag, and pop an element from the stack when the end tag is encountered, thus traversing to the specified length, The tag left in the stack is the label that needs to be filled or deleted.

Here is the code implementation, if everyone has a better way please give it:
Copy Code code as follows:

static char End_slash = '/';

<summary>
Secure truncated string
</summary>
<param name= "Input" > Input string </param>
<param name= "Length" > Truncation length </param>
<param name= "Trimhalftag" >true: truncated half label; false: Full half label </param>
<param name= "Tagstartchar" > tag start character </param>
<param name= "Tagendchar" > tag end character </param>
<param name= "Mustclosetags" > tag array to close </param>
<returns>length Length of String </returns>
public static string Safetrim (string input, int length, bool Trimhalftag, Char Tagstartchar, Char Tagendchar, string[] Mus Tclosetags)
{
if (length <= 0) throw new ArgumentException ("Length must be a positive number");
if (mustclosetags = = null) throw new ArgumentNullException ("Mustclosetags");

int Inputlen = input. Length;
if (string. IsNullOrEmpty (input) | | Inputlen <= length) return input;

string result = String. Empty;

The declaration stack is used to put labels
stack<string> tags = new stack<string> ();

for (int i = 0; i < length; i++)
{
char C = input[i];

if (c = = Tagstartchar)
{
string tag = string. Empty;
int tagindex = i + 1;
BOOL Istagend = false;
BOOL Istagnameend = false;
result = C;
BOOL Hasmarktaginstack = false;
while (Tagindex < Inputlen)
{
Char TAGC = Input[tagindex];
result = TAGC;
tagindex++;
if (tag = = string. Empty && TAGC = = End_slash)
{
Istagend = true;
Continue
}
if (!istagnameend)
{
if (char. Isletter (TAGC) | | Char. Isnumber (TAGC))
{
Tag + TAGC;
}
Else
{
Istagnameend = true;
}
}

if (!string. IsNullOrEmpty (TAG))
{
if (istagnameend &&!hasmarktaginstack)
{
if (istagend)
{
Tags. Pop ();
}
Else
{
Tags. Push (tag);
}
Hasmarktaginstack = true;
}
}

if (istagnameend)
{
if (TAGC = = Tagendchar)
{
i = tagIndex-1;
Break
}
}

}
}
Else
{
result = C;
}
}

while (tags. Count > 0)
{
String tag = tags. Pop ();

BOOL Ismustclosetag = false;
foreach (String Mustclosetag in Mustclosetags)
{
if (String.Compare (Mustclosetag, tag, true) = = 0)
{
Ismustclosetag = true;
Break
}
}
if (Ismustclosetag)
{
if (Trimhalftag)
{
int Lasttagindex = result. LastIndexOf (tagstartchar.tostring () + tag, stringcomparison.currentcultureignorecase);

result = result. Substring (0, Lasttagindex);
}
Else
{
result = = (tagstartchar.tostring () + End_slash + tag + tagendchar);
}
}
}

return result;
}

Reprint please keep the link Technology Blog of Jade Open
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.