Asp.net string base class operations (security, alternatively, decomposition)

/ ############################################
Copyright:
content of the article is the site editor, you can create any reprint, publish, but be sure to use plain text marked the original source and this statement.
http://www.opent.cn   author: Waves
########## ################################## /

/ ************************************************* *********************************
 *
 * function: common base class
 * author: Liu Gongxun;
 * version : V0.1 (C # 2.0); time: 2006-8-13
 *
 * ******************************* **************************** /

/ ************************************************* **************
* update record
* 2007-1-5 update:
* 1, a string of several characters take the right
* 2, the replacement string is the right
**** ************************************************** ********** /

the using the System;
the using the System.Data;
the using the System.Configuration;
the using the System.Web;
the using the System.Web.Security;
the using System.Web.UI;
the using System.Web.UI. WebControls;
a using System.Web.UI.WebControls.WebParts;
a using System.Web.UI.HtmlControls;
a using System.Text;

EC namespace
{
    /// <Summary>
    /// common base class
    /// </ Summary>
    public class FunObject
    {
        #region replacement string
        /// <Summary>
        /// function: substitution character
        /// </ Summary>
        /// <param name = "strValue"> string </ param>
        /// <Returns> replace 'string </ Returns>
        public static string FilterSQL (string strValue)
        {
            string STR = "";
            = strVAlue.Replace STR ( " ''", "");
            return STR;
        }
        #endregion

        #region table form content HTML conversion operation,
        /// <Summary>
        /// functions: table form content HTML conversion operation,
        /// </ Summary>
        /// <param name = "fString"> HTML string </ param>
        /// <Returns> </ Returns>
        public static string HTMLCode (string fString)
        {
            string STR = "";
            STR = fString.Replace ( ">", "& gt;");
            STR = fString .replace ( "<", "& lt;");
            STR = fString.Replace ( "", "& nbsp;");
            STR = fString.Replace ( "\ n-", "<br />");
            STR = fString .Replace ( "\ r", " <br />");
            str = fString.Replace("\r\n", "<br />");

            return str;
        }
#endregion

        #region 判断是否:返回值:√ or ×
        /// <summary>
        /// 判断是否:返回值:√ or ×
        /// </summary>
        /// <param name="b">true 或false</param>
        /// <returns>√ or ×</returns>
        public static string Judgement(bool b)
        {
            string s = "";
            if (b == true)
                s = "<b><font color=#009900>√</font></b>";
            else
                s = "<b><font color=#FF0000>×</font></b>";
            return s;
        }
        #endregion

        #region interception string
        /// <Summary>
        /// function: intercepting string length
        /// </ Summary>
        /// <param name = "STR"> character string to be taken </ param>
        /// <param name = "length"> string length </ param>
        /// <param name = "FLG"> to true: add ..., flase: without </ param>
        /// <Returns> </ Returns >
        public static String the GetString (String STR, int length, BOOL FLG)
        {
            int I = 0, J = 0;
            the foreach (CHR char in STR)
            {
                IF ((int) CHR> 127)
                {
                    I + = 2;
                }
                Else
                {
                    i ++;
                }
                if (i > length)
                {
                    str = str.Substring(0, j);
                    if (flg)
                        str += "......";
                    break;
                }
                j++;
            }
            return str;
        }
        #endregion

        #region 截取字符串+…
        /// <summary>
        /// 截取字符串+…
        /// </summary>
        /// <param name="strInput"></param>
        /// <param name="intlen"></param>
        /// <returns></returns>
        public static string CutString(string strInput, int intlen)//截取字符串
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int intLength = 0;
            string strString = "";
            byte[] s = ascii.GetBytes(strInput);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    intLength += 2;
                }
                else
                {
                    intLength += 1;
                }

                try
                {
                    strString += strInput.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                IF (intLength> intlen)
                {
                    BREAK;
                }
            }
            // if cut too plus half the ellipsis
            byte [] = MyByte System.Text.Encoding.Default.GetBytes (strInput);
            IF (mybyte.Length> intlen)
            {
                strString = + "...";
            }
            return strString;
        }
        #endregion

        #region 字符串分函数
        /// <summary>
        /// 字符串分函数
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="index"></param>
        /// <param name="Separ"></param>
        /// <returns></returns>
        public string StringSplit(string strings, int index, string Separ)
        {
            string[] s = strings.Split(char.Parse(Separ));
            return s[index];
        }
#endregion

        #region resolving string array
        /// <Summary>
        /// string partition function
        /// </ Summary>
        /// <param name = "STR"> character string to be decomposed </ param>
        /// <param name = "splitstr"> delimiter may be a string type </ param>
        /// <Returns> character array </ Returns>
        public static string [] splitstr (STR string, string splitstr)
        {
            IF (splitstr! = "")
            {
                The System.Collections.ArrayList The System.Collections.ArrayList new new C = ();
                the while (to true)
                {
                    int = thissplitindex str.IndexOf (splitstr);
                    IF (thissplitindex>= 0)
                    {
                        c.Add(str.Substring(0, thissplitindex));
                        str = str.Substring(thissplitindex + splitstr.Length);
                    }
                    else
                    {
                        c.Add(str);
                        break;
                    }
                }
                string[] d = new string[c.Count];
                for (int i = 0; i < c.Count; i++)
                {
                    d[i] = c[i].ToString();
                }
                return d;
            }
            else
            {
                return new string[] { str };
            }
        }
        #Endregion

        #region URL编码
        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string UrlEncoding(string str)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
            return System.Text.Encoding.UTF8.GetString(bytes).ToString();
        }
        #endregion

        #region obtaining configuration field value in Web.config
        /// <Summary>
        /// Get global parameters
        /// </ Summary>
        /// <param name = "Key"> keys </ param>
        // / <returns> parameters </ Returns>
        public static String getApp (String Key)
        {
            System.Configuration.AppSettingsReader new new System.Configuration.AppSettingsReader APPR = ();
            the try
            {
                String STR = (String) appr.GetValue (Key, typeof ( String));
                IF (STR STR == == null || "") return null;
                return STR;
            }
            the catch (Exception E) {}
            return null;
        }

        #endregion

        #region whether the yes / no return from the passed string Bit
        /// <Summary>
        /// whether the yes / no Bit returns the passed string
        /// </ Summary>
        /// <param name = "FLG"> </ param>
        /// <Returns> </ Returns>
        public static int GetBitBool (String FLG)
        {
            int STR = 0;
            Switch (flg.ToLower ())
            {
                Case "Yes":
                    STR. 1 = ;
                    BREAK;
                Case "NO":
                    STR = 0;
                    BREAK;
                default:
                    BREAK;
            }
            return str;
        }
        #endregion

        #region Html编码
        /// <summary>
        /// HTML编码
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static string HtmlEncode(string strInput)
        {
            string str;
            try
            {
                str = HttpContext.Current.Server.HtmlEncode(strInput);
            }
            catch
            {
                str = "error";
            }
            return str;
        }
        /// <summary>
        /// HTML解码
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static string HtmlDecode(string strInput)
        {
            string str;
            try
            {
                str = HttpContext.Current.Server.HtmlDecode(strInput);
            }
            catch
            {
                str = "error";
            }
            return str;
        }
        #endregion      

 #region detecting a break character, whether or not another character exists, returns true exists, otherwise to false
        /// <Summary>
        /// detecting a break character, whether or not another character exists, returns true presence, otherwise to false
        /// </ Summary>
        /// <param name = "of srcString"> original string </ param>
        /// <param name = "aimString"> the target string </ param>
        /// < Returns> </ Returns>
        public static BOOL IsEnglish (of srcString String, String aimString)
        {
            BOOL Rev = to true;
            String CHR;
            IF (aimString == "" || aimString == null) return to false;
            for (int I = 0; I <aimString.Length; I ++)
            {
                CHR = aimString.Substring (I,. 1);
                if (srcString.IndexOf(chr) < 0)
                {
                    return false;
                    break;
                }

            }
            return Rev;
        }
        #endregion

        #region detect whether it contains the string length Chinese and Chinese
        /// <Summary>
        /// detect whether it contains the string length Chinese and Chinese
        /// </ Summary>
        /// <param name = "STR"> to string detected </ param>
        /// <Returns> Chinese string length </ Returns>
        public static int CnStringLength (string STR)
        {
            the ASCIIEncoding the ASCIIEncoding n-new new = ();
            byte [] B = n.GetBytes (STR) ;
            int L = 0; // L is the actual length of the string
            for (int I = 0; I <= b.length -. 1; I ++)
            {
                iF (B [I] == 63 is) // determines whether characters the whole foot or symbols
                {
                    L ++;
                }
            }
            return L;
       
        }
        #endregion  

 #region take the right string of several characters
        /// <Summary>
        /// string of several characters take the right
        /// </ Summary>
        /// <param name = "STR"> string < / param>
        /// <param name = few characters of "length"> the right </ param>
        /// <Returns> </ Returns>
        public static String GetStrRight (STR String, int length)
        {
            String = Rev " ";

            if (str.Length < length)
            {
                Rev = str;

            }
            else
            {
                Rev = str.Substring(str.Length - length, length);
            }
            return Rev;


        }
        #endregion

        replacement string is the right #region

        /// <Summary>
        /// replacement string is the right
        /// </ Summary>
        /// <param name = "STR"> string </ param>
        /// <param name = "strsrc"> the string right </ param>
        /// <param name = "straim"> to replace the string </ param>
        /// <Returns> </ Returns>
        public static string RepStrRight (STR string, string strsrc , String straim)
        {

            string Rev = "";
            if (GetStrRight(str, strsrc.Length) != strsrc)
            {
                Rev = str;
            }
            else
            {
                Rev = str.Substring(0, str.Length - strsrc.Length).ToString() + straim.ToString();
            }
            return Rev;
        }
        #endregion
    }

}

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2007/05/05/736703.html

Guess you like

Origin blog.csdn.net/weixin_34007886/article/details/93495925