Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. Web. sessionstate;
Using system. xml;
Using system. Security;
Using system. net;
Using system. Text. regularexpressions;
/// <Summary>
/// Summary of stringutil
/// String processing, character conversion
/// </Summary>
Public class stringutil
{
Public stringutil ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Delete the specified string from the end of the string
/// </Summary>
/// <Param name = "sourcestring"> </param>
/// <Param name = "removedstring"> </param>
/// <Returns> </returns>
Public static string remove (string sourcestring, string removedstring)
{
Try
{
If (sourcestring. indexof (removedstring) <0)
Throw new exception ("the original string does not contain the removed string! ");
String result = sourcestring;
Int lengthofsourcestring = sourcestring. length;
Int lengthofremovedstring = removedstring. length;
Int startindex = lengthofsourcestring-lengthofremovedstring;
String tempsubstring = sourcestring. substring (startindex );
If (tempsubstring. toupper () = removedstring. toupper ())
{
Result = sourcestring. Remove (startindex, lengthofremovedstring );
}
Return result;
}
Catch
{
Return sourcestring;
}
}
/// <Summary>
/// Obtain the string on the right of the separator
/// </Summary>
/// <Param name = "sourcestring"> </param>
/// <Param name = "splitchar"> </param>
/// <Returns> </returns>
Public static string rightsplit (string sourcestring, char splitchar)
{
String result = NULL;
String [] tempstring = sourcestring. Split (splitchar );
If (tempstring. length> 0)
{
Result = tempstring [tempstring. Length-1]. tostring ();
}
Return result;
}
/// <Summary>
/// Obtain the string on the left of the separator
/// </Summary>
/// <Param name = "sourcestring"> </param>
/// <Param name = "splitchar"> </param>
/// <Returns> </returns>
Public static string leftsplit (string sourcestring, char splitchar)
{
String result = NULL;
String [] tempstring = sourcestring. Split (splitchar );
If (tempstring. length> 0)
{
Result = tempstring [0]. tostring ();
}
Return result;
}
/// <Summary>
/// Remove the last comma
/// </Summary>
/// <Param name = "Origin"> </param>
/// <Returns> </returns>
Public static string dellastcomma (string origin)
{
If (origin. indexof (",") =-1)
{
Return origin;
}
Return origin. substring (0, origin. lastindexof (","));
}
/// <Summary>
/// Delete invisible characters
/// </Summary>
/// <Param name = "sourcestring"> </param>
/// <Returns> </returns>
Public static string deleteunvisiblechar (string sourcestring)
{
System. Text. stringbuilder sbuilder = new system. Text. stringbuilder (131 );
For (INT I = 0; I <sourcestring. length; I ++)
{
Int Unicode = sourcestring [I];
If (UNICODE> = 16)
{
Sbuilder. append (sourcestring [I]. tostring ());
}
}
Return sbuilder. tostring ();
}
/// <Summary>
/// Obtain the merged string of the array element
/// </Summary>
/// <Param name = "stringarray"> </param>
/// <Returns> </returns>
Public static string getarraystring (string [] stringarray)
{
String totalstring = NULL;
For (INT I = 0; I <stringarray. length; I ++)
{
Totalstring = totalstring + stringarray [I];
}
Return totalstring;
}
/// <Summary>
/// Obtain the number of times a string appears in the string array
/// </Summary>
/// <Param name = "stringarray" type = "string []">
/// <Para>
///
/// </Para>
/// </Param>
/// <Param name = "findstring" type = "string">
/// <Para>
///
/// </Para>
/// </Param>
/// <Returns>
/// A int value...
/// </Returns>
Public static int getstringcount (string [] stringarray, string findstring)
{
Int COUNT =-1;
String totalstring = getarraystring (stringarray );
String substring = totalstring;
While (substring. indexof (findstring)> = 0)
{
Substring = totalstring. substring (substring. indexof (findstring ));
Count + = 1;
}
Return count;
}
/// <Summary>
/// Obtain the number of times a string appears in the string
/// </Summary>
/// <Param name = "stringarray" type = "string">
/// <Para>
/// Original string
/// </Para>
/// </Param>
/// <Param name = "findstring" type = "string">
/// <Para>
/// Match string
/// </Para>
/// </Param>
/// <Returns>
/// Number of matched strings
/// </Returns>
Public static int getstringcount (string sourcestring, string findstring)
{
Int COUNT = 0;
Int findstringlength = findstring. length;
String substring = sourcestring;
While (substring. indexof (findstring)> = 0)
{
Substring = substring. substring (substring. indexof (findstring) + findstringlength );
Count + = 1;
}
Return count;
}
/// <Summary>
/// Intercept all characters starting from startstring to the end of the original string
/// </Summary>
/// <Param name = "sourcestring" type = "string">
/// <Para>
///
/// </Para>
/// </Param>
/// <Param name = "startstring" type = "string">
/// <Para>
///
/// </Para>
/// </Param>
/// <Returns>
/// A string value...
/// </Returns>
Public static string getsubstring (string sourcestring, string startstring)
{
Try
{
Int Index = sourcestring. toupper (). indexof (startstring );
If (index> 0)
{
Return sourcestring. substring (INDEX );
}
Return sourcestring;
}
Catch
{
Return "";
}
}
Public static string getsubstring (string sourcestring, string beginremovedstring, string endremovedstring)
{
Try
{
If (sourcestring. indexof (beginremovedstring )! = 0)
Beginremovedstring = "";
If (sourcestring. lastindexof (endremovedstring, sourcestring. Length-endremovedstring. Length) <0)
Endremovedstring = "";
Int startindex = beginremovedstring. length;
Int length = sourcestring. Length-beginremovedstring. Length-endremovedstring. length;
If (length> 0)
{
Return sourcestring. substring (startindex, length );
}
Return sourcestring;
}
Catch
{
Return sourcestring ;;
}
}
/// <Summary>
/// Extract the length of a string by the number of bytes
/// </Summary>
/// <Param name = "strtmp"> string to be calculated </param>
/// <Returns> Number of bytes of the string </returns>
Public static int getbytecount (string strtmp)
{
Int intcharcount = 0;
For (INT I = 0; I <strtmp. length; I ++)
{
If (system. Text. utf8encoding. utf8.getbytecount (strtmp. substring (I, 1) = 3)
{
Intcharcount = intcharcount + 2;
}
Else
{
Intcharcount = intcharcount + 1;
}
}
Return intcharcount;
}
/// <Summary>
/// The number of bytes must be at the position of the string
/// </Summary>
/// <Param name = "intins"> string position </param>
/// <Param name = "strtmp"> string to be calculated </param>
/// <Returns> byte location </returns>
Public static int getbyteindex (INT intins, string strtmp)
{
Int intreins = 0;
If (strtmp. Trim () = "")
{
Return intins;
}
For (INT I = 0; I <strtmp. length; I ++)
{
If (system. Text. utf8encoding. utf8.getbytecount (strtmp. substring (I, 1) = 3)
{
Intreins = intreins + 2;
}
Else
{
Intreins = intreins + 1;
}
If (intreins> = intins)
{
Intreins = I + 1;
Break;
}
}
Return intreins;
}
/// <Summary>
/// Method to make sure that user's inputs are not malicious
/// Capture the largest input string
/// </Summary>
/// <Param name = "text"> User's input </param>
/// <Param name = "maxlength"> Maximum length of input </param>
/// <Returns> the cleaned up version of the input </returns>
Public static string inputtext (string text, int maxlength)
{
TEXT = text. Trim ();
If (string. isnullorempty (text ))
Return string. empty;
If (text. length> maxlength)
TEXT = text. substring (0, maxlength );
TEXT = RegEx. Replace (text, "[// s] {2,}", ""); // two or more spaces
TEXT = RegEx. replace (text, "(<[B | B] [R | r]/*>) + | (<[p | p] (. | // n) *?>) ","/N "); // <br>
TEXT = RegEx. replace (text, "(// s * & [n | n] [B | B] [S | S] [p | p]; // s *) + ", ""); // & nbsp;
TEXT = RegEx. Replace (text, "<(. | // n) *?> ", String. Empty); // any other tags
TEXT = text. Replace ("'","''");
Return text;
}
/// <Summary>
/// Method to check whether input has other characters than numbers
/// Clear characters
/// </Summary>
Public static string cleannonword (string text)
{
Return RegEx. Replace (text, "// W ","");
}
// Convert special characters
Public static string encode (string Str)
{
STR = Str. Replace ("'","'");
STR = Str. Replace ("/" "," & quot ;");
STR = Str. Replace ("<", "& lt ;");
STR = Str. Replace (">", "& gt ;");
Return STR;
}
Public static string decode (string Str)
{
STR = Str. Replace ("& gt;", "> ");
STR = Str. Replace ("& lt;", "<");
STR = Str. Replace ("& nbsp ;","");
STR = Str. Replace ("& quot ;","/"");
Return STR;
}
// Character transfer conversion is used to prevent malicious password theft during query and login.
Public static string tbcode (string strtb)
{
Strtb = strtb. Replace ("! ","");
Strtb = strtb. Replace ("@","");
Strtb = strtb. Replace ("#","");
Strtb = strtb. Replace ("$ ","");
Strtb = strtb. Replace ("% ","");
Strtb = strtb. Replace ("^ ","");
Strtb = strtb. Replace ("&","");
Strtb = strtb. Replace ("*","");
Strtb = strtb. Replace ("(","");
Strtb = strtb. Replace (")","");
Strtb = strtb. Replace ("_","");
Strtb = strtb. Replace ("+ ","");
Strtb = strtb. Replace ("| ","");
Strtb = strtb. Replace ("? ","");
Strtb = strtb. Replace ("/","");
Strtb = strtb. Replace (".","");
Strtb = strtb. Replace ("> ","");
Strtb = strtb. Replace ("<","");
Strtb = strtb. Replace ("{","");
Strtb = strtb. Replace ("}","");
Strtb = strtb. Replace ("[","");
Strtb = strtb. Replace ("]", "");
Strtb = strtb. Replace ("-","");
Strtb = strtb. Replace ("= ","");
Strtb = strtb. Replace (",","");
Return strtb;
}
// Output the prompt information using JavaScript's us. Alert () method
// Strmsg indicates the information to be output
// Back indicates whether history. Back () is required after the output ()
// End indicates whether response. End () is required after the output ()
Public static void writemessage (string strmsg, bool back, bool end)
{
String JS = "";
// Response = httpcontext. Current. response;
// Replace single quotes to prevent JS errors
Strmsg = strmsg. Replace ("'","");
// Replace the carriage return symbol to prevent JS errors.
Strmsg = strmsg. Replace ("/R/N ","");
JS = "<script language = JavaScript> alert ('" + strmsg + "') </SCRIPT> ";
If (back)
{
JS = "<script language = JavaScript> history. Back (); </script )";
}
If (end)
{
JS = "<script language = JavaScript> end </script )";
}
Httpcontext. Current. response. Write (JS );
}
/// <Summary>
/// Small function, which converts a string to the float type. If the string is empty, 0 is returned.
/// </Summary>
/// <Param name = "str"> string to be converted </param>
/// <Returns> converted float value </returns>
Public static float getfloatvalue (string Str)
{
Try
{
If (Str. Length = 0)
Return 0;
Else
Return float. parse (STR );
}
Catch
{
Writemessage ("Data Conversion failed. Please check whether the data is reasonable! ", True, true );
Return 0;
}
}
/// <Summary>
/// Small function, which converts a string to int type. If the string is null, 0 is returned.
/// </Summary>
/// <Param name = "str"> string to be converted </param>
/// <Returns> converted int value </returns>
Public static int getintvalue (string Str)
{
Try
{
If (Str. Length = 0)
Return 0;
Else
{
If (int32.parse (STR) = 0)
Return 0;
Else
Return int32.parse (STR );
}
}
Catch
{
Writemessage ("Data Conversion failed. Please check whether the data is reasonable! ", True, true );
Return 0;
}
}
}