程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 一些C#實用的方法匯總,

一些C#實用的方法匯總,

編輯:C#入門知識

一些C#實用的方法匯總,


/// <summary>
    /// 過濾字符串方法,用於將單引號等特殊符號轉化成中文符號
    /// </summary>
    /// <param name="msg">要轉化的字符串</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 result;
    }



    //彈出消息框
    /// <summary>
    /// 消息提示框
    /// </summary>
    /// <param name="msg">消息內容</param>
    public static void ShowMsgBox(string msg) 
    {
       System.Web.HttpContext.Current.Response.Write("<script>alert('"+msg+"')</script>");
    }

    /// <summary>
    /// 彈出消息框,調用彈出消息框,不影響樣式buss.Showmessage(this.Page,"提交成功");
    /// </summary>
    /// <param name="Page">Page對像</param>
    /// <param name="msg">消息</param>
    public static void Showmessage(System.Web.UI.Page 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>
    /// 截取字符串
    /// </summary>
    /// <param name="msg">原字符串</param>
    /// <param name="lenth">要截取的長度</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>
    /// 剪切指定長度的字符串,並去掉HTML標記
    /// </summary>
    /// <param name="strr">要剪切字符串的Object形式</param>
    /// <param name="len">長度(中文長度為2)</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];
            nPos++;

            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;
    }


    //是否為中文
    public static bool IsChinese(char ch)
    {
        ASCIIEncoding en = new ASCIIEncoding();
        byte[] b = en.GetBytes(ch.ToString());
        return (b[0] == 63);
    }

    //HTML標記轉換
    public static string HTMLEncode(string str)
    {
        str = str.Replace("<", "<");
        str = str.Replace(">", ">");
        str = str.Replace("\n", "<br/>");

        return str;
    }


    /// <summary>
    /// 把日期時間轉換為日期,去掉時間
    /// </summary>
    /// <param name="str">格式:YYYY-MM-DD HH:MM:SS</param>
    /// <returns>返回日期 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];
    }

 //隨機函數產生字符   
    public static string CreateRandomCode(int codeCount, string allChar)
    {
        //驗證碼中的出現的字符,避免了一些容易混淆的字符。 
        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;
    }

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved