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