Any code in ASP. NET replacing HTML code <>
Replace any code in <>.
The method is as follows:
Protected string dealto (string procstr)
{
String [] strarr = {"onclick", "onmouseover", "onmouseout", "ondblclick", "ondisposed", "oninit", "onkeydown", "onkeyup ", "onkeypress", "onLoad", "onmousedown", "onmousemove", "onmouseup", "onperender", "onUnload "};
// Code array to be replaced, which can be customized
String replaceto = "RPT ";
String restr = "";
Restr = procstr;
For (INT I = 0; I <strarr. length; I ++)
{
Restr = checkanddeal (restr, strarr [I], replaceto );
}
Return restr;
}
Protected string checkanddeal (string checkstr, string repstr, string replaceto)
{
Int strindex;
Int strlen = 0;
Strlen = checkstr. length;
String returnstr = "";
String tempreplaceto = "asdfghjkl ";
// Temporarily replace the string, which can be replaced by any string, but it must be ensured that the string does not appear in the string to be processed
If (strlen <= 0)
{
Return "";
}
Strindex = checkstr. indexof (repstr );
If (strindex! =-1) // No need to judge if the value is equal to or less than 0. You can directly return the original string.
{
While (checkstr. indexof (repstr )! =-1) // cyclically determine whether the string can be matched and replaced.
{
Int indexofgreat =-1;
Int indexofless =-1;
Strindex = checkstr. indexof (repstr );
Char [] Charr = checkstr. tochararray ();
// Search for characters <and>
Char coms = '';
For (INT I = strindex-1; I> = 0; I --)
{
// String S = checkstr. substring (I, 1 );
Coms = Charr [I];
If (COMS = '> ')
{
Break;
}
Else if (COMS = '<')
{
Indexofless = I;
Break;
}
}
For (INT I = strindex; I <strlen; I ++)
{
// String S = checkstr. substring (I, 1 );
Coms = Charr [I];
If (COMS = '<')
{
Break;
}
Else if (COMS = '> ')
{
Indexofgreat = I;
Break;
}
}
String subleft = "";
String subright = "";
Subleft = checkstr. substring (0, checkstr. indexof (repstr ));
Subright = checkstr. substring (checkstr. indexof (repstr) + repstr. Length ));
If (indexofgreat>-1 & indexofless>-1 & indexofgreat> indexofless & indexofgreat <strlen)
// If yes, replace it with a temporary string. Otherwise, replace the searched string with a temporary string.
{
Checkstr = subleft + replaceto + subright;
}
Else
{
Checkstr = subleft + tempreplaceto + subright;
}
}
Checkstr = checkstr. Replace (tempreplaceto, repstr); // replace the temporary string with the original string.
Returnstr = checkstr;
}
Else
{
Returnstr = checkstr;
}
Return returnstr;
}