Summary of some C # practical methods,
/// <Summary> /// string filtering method, converts special characters such as single quotes to Chinese characters. // </summary> /// <param name = "msg"> string to be converted </param> public static string FilterStr (string msg) {string result = ""; // msg = msg. replace (","); // msg = msg. replace ("<", "<"); // msg = msg. replace (">", ">"); // msg = msg. replace ("\ n", "<br>"); // msg = msg. replace ("\" "," "); msg = msg. trim (); msg = msg. replace ("'", "'' "); result = msg; return result ;} Public static void message (string meg) {} public static string FilterStrHtml (string msg) {string result = ""; msg = msg. replace ("\ '", "'"); // msg = msg. replace (","); msg = msg. replace ("\ n", "<br>"); result = msg; return result;} public static string FilterStrnocode (string msg) {string result = ""; msg = msg. replace ("'", "\'"); msg = msg. replace ("<br>", "\ n"); result = msg; return resu Lt ;} // pop-up message box /// <summary> /// message prompt box // </summary> /// <param name = "msg"> message content </param> public static void ShowMsgBox (string msg) {System. web. httpContext. current. response. write ("<script> alert ('" + msg + "') </script>");} // <summary> // the message box is displayed, call the pop-up message box without affecting the style buss. showmessage (this. page, "submitted successfully "); /// </summary> /// <param name = "Page"> Page Object </param> /// <param name = "msg"> message </param> public static void Show Message (System. web. UI. page, string msg) {// ClientScript. registerStartupScript (GetType (), "JS1", "alert ('" + msg + "')", true); // Page. clientScript. registerStartupScript (Page. getType (), "JS1", "alert ('" + msg + "')", true); Page. clientScript. registerStartupScript (Page. getType (), "", "<script lanuage = javascript> alert ('" + msg + "') </script> ");} /// <summary> /// capture the string /// </summary> /// <pa Ram name = "msg"> original string </param> /// <param name = "lenth"> length to be intercepted </param> /// <returns> </ returns> public static string ReturnStr (string msg, int lenth) {string result = msg; if (msg. length> lenth) {result = result. substring (0, lenth);} // http://www.cnblogs.com/sosoft/ return result ;} /// <summary> ///// </summary> /// <param name = "sqlstr"> </param> /// <returns> </returns> /// <summary> /// cut the specified length String, and remove the HTML Tag /// </summary> /// <param name = "strr"> the Object format of the string to be cut </param> /// <param name =" len "> length (2 in Chinese) </param> // <returns> </returns> public static string CutStringX (object strr, int len) {string str = strr. toString (); string strRet = ""; char CH; int nLen = str. length; int nCutLen = 0; int nPos = 0; int bLeft = 0; int nChinese = 0; while (nPos <nLen & nCutLen <len) {CH = str [nPos]; nP OS ++; if (CH = '<') {bLeft ++; continue;} if (CH = '>') {bLeft --; continue ;} if (nCutLen = 0 & CH. toString () = "" & CH. toString () = "\ n") {continue;} if (bLeft = 0) {// if (IsChinese (CH) {nCutLen + = 2; nChinese ++;} else {nCutLen + = 1;} strRet + = CH;} strRet = strRet. replace ("", ""); if (nPos <nLen) {strRet + = ""; // strRet + = "... ";} return strRet;} // whether it is Chinese public Static bool IsChinese (char ch) {ASCIIEncoding en = new ASCIIEncoding (); byte [] B = en. getBytes (ch. toString (); return (B [0] = 63);} // convert public static string HTMLEncode (string str) {str = str. replace ("<", "<"); str = str. replace (">", ">"); str = str. replace ("\ n", "<br/>"); return str ;}/// <summary> // converts a date to a date, remove time /// </summary> /// <param name = "str"> Format: YYYY-MM-DD HH: MM: SS </param> /// <Returns> return date YYYY-MM-DD </returns> public static string GetDate (string str) {string [] s = str. split (''); string [] ss = s [0]. split ('-'); if (ss [1]. length <2) ss [1] = "0" + ss [1]; if (ss [2]. length <2) ss [2] = "0" + ss [2]; return ss [0] + "-" + ss [1] + "-" + ss [2];} // The public static string CreateRandomCode (int codeCount, string allChar) {// characters in the verification code to avoid obfuscation. If (string. isNullOrEmpty (allChar) allChar = "3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, J, K, M, N, p, Q, R, S, T, U, W, X, Y "; string [] allCharArray = allChar. split (','); string randomCode = ""; int temp =-1; bool breCreate = (codeCount <6 & allCharArray. length> 15); Random rand = new Random (); for (int I = 0; I <codeCount; I ++) {if (temp! =-1) {rand = new Random (I * temp * (int) DateTime. now. ticks);} int t = rand. next (allCharArray. length); if (temp = t & breCreate) {return CreateRandomCode (codeCount, allChar);} temp = t; randomCode + = allCharArray [t];} return randomCode ;}