Method: remove dangerous characters using functions:
1. Create a temporary String Array for saving strings
2. Get the length of the string
3. replace dangerous characters one by one
4. Return the processed string
Eg.
Public static string clearstringinput (string sinputstring, int imaxlength)
{
// Construct a temporary String Array
Stringbuilder retval = new stringbuilder ();
If (sinputstring! = NULL) & sinputstring! = String. Empty)
{
// Clear the white space at both ends of the string
Sinputstring = sinputstring. Trim ();
// Set the length of the string
If (sinputstring. length> imaxlength)
{
Sinputstring = sinputstring. substring (0, imaxlength );
}
For (INT I = 0; I <imaxlength; I ++)
{
Switch (sinputstring [I])
{
// Replace dangerous characters
Case '"': retval. append (" & quot; "); break;
Case '<': retval. append ("& lt;"); break;
Case '>': retval. append ("& gt"); break;
Default: retval. append (sinputstring [I]); break;
}
}
// Replace the character "'"
Retval. Replace ("'","");
}
// Return the processed string
Return retval. tostring ();
}