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

StrHelper,

編輯:C#入門知識

StrHelper,


public class StrHelper
{
    private static string passWord;	//加密字符串

    /// <summary>
    /// 判斷輸入是否數字
    /// </summary>
    /// <param name="num">要判斷的字符串</param>
    /// <returns></returns>
    static public bool VldInt(string num)
    {
        #region
        int ResultNum;
        return int.TryParse(num, out ResultNum);
        #endregion
    }

    /// <summary>
    /// 返回文本編輯器替換後的字符串
    /// </summary>
    /// <param name="str">要替換的字符串</param>
    /// <returns></returns>
    static public string GetHtmlEditReplace(string str)
    {
        #region
        return str.Replace("'", "’").Replace(" ", " ").Replace(",", ",").Replace("%", "%").
            Replace("script", "").Replace(".js", "");
        #endregion
    }

    /// <summary>
    /// 截取字符串函數
    /// </summary>
    /// <param name="str">所要截取的字符串</param>
    /// <param name="num">截取字符串的長度</param>
    /// <returns></returns>
    static public string GetSubString(string str, int num)
    {
        #region
        return (str.Length > num) ? str.Substring(0, num) + "..." : str;
        #endregion
    }

    /// <summary>
    /// 截取字符串優化版
    /// </summary>
    /// <param name="stringToSub">所要截取的字符串</param>
    /// <param name="length">截取字符串的長度</param>
    /// <returns></returns>
    public static string GetFirstString(string stringToSub, int length)
    {
        #region
        var regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
        var stringChar = stringToSub.ToCharArray();
        var sb = new StringBuilder();
        var nLength = 0;
        var isCut = false;
        for (var i = 0; i < stringChar.Length; i++)
        {
            if (regex.IsMatch((stringChar[i]).ToString()))
            {
                sb.Append(stringChar[i]);
                nLength += 2;
            }
            else
            {
                sb.Append(stringChar[i]);
                nLength = nLength + 1;
            }

            if (nLength > length)
            {
                isCut = true;
                break;
            }
        }
        if (isCut)
            return sb.ToString() + "..";
        else
            return sb.ToString();
        #endregion
    } 

    /// <summary>
    /// 過濾輸入信息
    /// </summary>
    /// <param name="text">內容</param>
    /// <param name="maxLength">最大長度</param>
    /// <returns></returns>
    public static string InputText(string text, int maxLength)
    {
        #region
        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*)+", " ");	// 
        text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);	//any other tags
        text = text.Replace("'", "''");
        return text;
        #endregion
    }

    /// <summary>
    /// 生成隨機數
    /// </summary>
    /// <returns></returns>
    private string GenerateCheckCode()
    {
        #region
        int number;
        char code;
        string checkCode = String.Empty;

        System.Random random = new Random();

        for (int i = 0; i < 5; i++)
        {
            number = random.Next();

            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();
        }

        HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

        return checkCode;
        #endregion
    }


    /// <summary>
    /// 生成驗證碼圖片
    /// </summary>
    public void CreateCheckCodeImage()
    {
        #region
        string checkCode = GenerateCheckCode();
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;

        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成隨機生成器
            Random random = new Random();

            //清空圖片背景色
            g.Clear(Color.White);

            //畫圖片的背景噪音線
            for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            var font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);

            //畫圖片的前景噪音點
            for (int i = 0; i < 150; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //畫圖片的邊框線
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            var ms = new MemoryStream();
            image.Save(ms, ImageFormat.Gif);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Gif";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
        #endregion
    }

    #region 生成指定位數隨機數
    private static char[] constant =   
        {   
        '0','1','2','3','4','5','6','7','8','9',   
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };

    public static string GenerateRandom(int Length)
    {
        var newRandom = new StringBuilder(62);
        var rd = new Random();
        for (int i = 0; i < Length; i++)
        {
            newRandom.Append(constant[rd.Next(62)]);
        }
        return newRandom.ToString();
    }

    public static string GetNumRandom(int Length)
    {
        var newRandom = new StringBuilder(10);
        char[] NumStr={'0','1','2','3','4','5','6','7','8','9'};
        var rd = new Random();
        for (int i = 0; i < Length; i++)
        {
            newRandom.Append(constant[rd.Next(10)]);
        }
        return newRandom.ToString();
    }
    #endregion

    /// <summary>
    /// 獲取漢字第一個拼音
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    static public string getSpells(string input)
    {
        #region
        int len = input.Length;
        string reVal = "";
        for (int i = 0; i < len; i++)
        {
            reVal += getSpell(input.Substring(i, 1));
        }
        return reVal;
        #endregion
    }

    static public string getSpell(string cn)
    {
        #region
        byte[] arrCN = Encoding.Default.GetBytes(cn);
        if (arrCN.Length > 1)
        {
            int area = (short)arrCN[0];
            int pos = (short)arrCN[1];
            int code = (area << 8) + pos;
            int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
            for (int i = 0; i < 26; i++)
            {
                int max = 55290;
                if (i != 25) max = areacode[i + 1];
                if (areacode[i] <= code && code < max)
                {
                    return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                }
            }
            return "?";
        }
        else return cn;
        #endregion
    }

    /// <summary>
    /// 半角轉全角
    /// </summary>
    /// <param name="BJstr"></param>
    /// <returns></returns>
    static public string GetQuanJiao(string BJstr)
    {
        #region
        char[] c = BJstr.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
            if (b.Length == 2)
            {
                if (b[1] == 0)
                {
                    b[0] = (byte)(b[0] - 32);
                    b[1] = 255;
                    c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                }
            }
        }

        string strNew = new string(c);
        return strNew;

        #endregion
    }

    /// <summary>
    /// 全角轉半角
    /// </summary>
    /// <param name="QJstr"></param>
    /// <returns></returns>
    static public string GetBanJiao(string QJstr)
    {
        #region
        char[] c = QJstr.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
            if (b.Length == 2)
            {
                if (b[1] == 255)
                {
                    b[0] = (byte)(b[0] + 32);
                    b[1] = 0;
                    c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                }
            }
        }
        string strNew = new string(c);
        return strNew;
        #endregion
    }

    #region 加密的類型
    /// <summary>
    /// 加密的類型
    /// </summary>
    public enum PasswordType
    {
        SHA1,
        MD5
    }
    #endregion
       
    /// <summary>
    /// 字符串加密
    /// </summary>
    /// <param name="PasswordString">要加密的字符串</param>
    /// <param name="PasswordFormat">要加密的類別</param>
    /// <returns></returns>
    static public string EncryptPassword(string PasswordString, PasswordType PasswordFormat)
    {
        #region
        switch (PasswordFormat)
        {
            case PasswordType.SHA1:
                {
                    passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
                    break;
                }
            case PasswordType.MD5:
                {
                    passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5").Substring(8, 16).ToLower();
                    break;
                }
            default:
                {
                    passWord = string.Empty;
                    break;
                }
        }
        return passWord;
        #endregion
    }

    /// <summary>
    /// 字符串轉換為 html
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string StringToHtml(string str)
    {
        #region
        str = str.Replace("&", "&");
        str = str.Replace(" ", " ");
        str = str.Replace("'", "''");
        str = str.Replace("\"", """);
        str = str.Replace(" ", " ");
        str = str.Replace("<", "<");
        str = str.Replace(">", ">");
        str = str.Replace("\r\n", "<br>");

        return str;
        #endregion
    }

    /// <summary>
    /// html轉換成字符串
    /// </summary>
    /// <param name="strHtml"></param>
    /// <returns></returns>
    public static string HtmlToString(string strHtml)
    {
        #region
        strHtml = strHtml.Replace("<br>", "\r\n");
        strHtml = strHtml.Replace(@"<br />", "\r\n");
        strHtml = strHtml.Replace(@"<br/>", "\r\n");
        strHtml = strHtml.Replace(">", ">");
        strHtml = strHtml.Replace("<", "<");
        strHtml = strHtml.Replace(" ", " ");
        strHtml = strHtml.Replace(""", "\"");

        strHtml = Regex.Replace(strHtml, @"<\/?[^>]+>", "", RegexOptions.IgnoreCase);

        return strHtml;
        #endregion
    }

    /// <summary>
    /// 獲得中文星期表示形式
    /// </summary>
    /// <returns></returns>
    public static string GetChineseWeek(DateTime t)
    {
        #region
        string week = "";

        switch (t.DayOfWeek)
        {
            case DayOfWeek.Monday:
                week = "一";
                break;
            case DayOfWeek.Tuesday:
                week = "二";
                break;
            case DayOfWeek.Wednesday:
                week = "三";
                break;
            case DayOfWeek.Thursday:
                week = "四";
                break;
            case DayOfWeek.Friday:
                week = "五";
                break;
            case DayOfWeek.Saturday:
                week = "六";
                break;
            case DayOfWeek.Sunday:
                week = "日";
                break;
        }

        return "星期" + week;
        #endregion
    }

    /// <summary>
    /// 生成日期隨機碼
    /// </summary>
    /// <returns></returns>
    public static string GetRamCode()
    {
        #region
        return DateTime.Now.ToString("yyyyMMddHHmmssff"); 
        #endregion
    }

    /// <summary>
    /// 生成指定長度的字符串,即生成strLong個str字符串
    /// </summary>
    /// <param name="strLong">生成的長度</param>
    /// <param name="str">以str生成字符串</param>
    /// <returns></returns>
    public static string StringOfChar(int strLong, string str)
    {
        #region 
        string ReturnStr = string.Empty;
        for (int i = 0; i < strLong; i++)
        {
            ReturnStr += str;
        }        
        return ReturnStr; 
        #endregion
    }

    public static string FilterStr(string str)
    {  
          
        return str.Replace("'", " ").Replace(".", "").Replace("\r\n", " ");
    }

    /// <summary>
    /// 生成條形碼:如:bar_code("20070520122334", 20, 1, 1);
    /// </summary>
    /// <param name="str"></param>
    /// <param name="ch">度度</param>
    /// <param name="cw">線條寬度</param>
    /// <param name="type_code">是否輸出文字1為輸出</param>
    /// <returns></returns>
    public static string BarCode(object str, int ch, int cw, int type_code)
    {
        #region
        string strTmp = str.ToString();
        string code = strTmp;
        strTmp = strTmp.ToLower();
        int height = ch;
        int width = cw;

        strTmp = strTmp.Replace("0", "_|_|__||_||_|"); ;
        strTmp = strTmp.Replace("1", "_||_|__|_|_||");
        strTmp = strTmp.Replace("2", "_|_||__|_|_||");
        strTmp = strTmp.Replace("3", "_||_||__|_|_|");
        strTmp = strTmp.Replace("4", "_|_|__||_|_||");
        strTmp = strTmp.Replace("5", "_||_|__||_|_|");
        strTmp = strTmp.Replace("7", "_|_|__|_||_||");
        strTmp = strTmp.Replace("6", "_|_||__||_|_|");
        strTmp = strTmp.Replace("8", "_||_|__|_||_|");
        strTmp = strTmp.Replace("9", "_|_||__|_||_|");
        strTmp = strTmp.Replace("a", "_||_|_|__|_||");
        strTmp = strTmp.Replace("b", "_|_||_|__|_||");
        strTmp = strTmp.Replace("c", "_||_||_|__|_|");
        strTmp = strTmp.Replace("d", "_|_|_||__|_||");
        strTmp = strTmp.Replace("e", "_||_|_||__|_|");
        strTmp = strTmp.Replace("f", "_|_||_||__|_|");
        strTmp = strTmp.Replace("g", "_|_|_|__||_||");
        strTmp = strTmp.Replace("h", "_||_|_|__||_|");
        strTmp = strTmp.Replace("i", "_|_||_|__||_|");
        strTmp = strTmp.Replace("j", "_|_|_||__||_|");
        strTmp = strTmp.Replace("k", "_||_|_|_|__||");
        strTmp = strTmp.Replace("l", "_|_||_|_|__||");
        strTmp = strTmp.Replace("m", "_||_||_|_|__|");
        strTmp = strTmp.Replace("n", "_|_|_||_|__||");
        strTmp = strTmp.Replace("o", "_||_|_||_|__|");
        strTmp = strTmp.Replace("p", "_|_||_||_|__|");
        strTmp = strTmp.Replace("r", "_||_|_|_||__|");
        strTmp = strTmp.Replace("q", "_|_|_|_||__||");
        strTmp = strTmp.Replace("s", "_|_||_|_||__|");
        strTmp = strTmp.Replace("t", "_|_|_||_||__|");
        strTmp = strTmp.Replace("u", "_||__|_|_|_||");
        strTmp = strTmp.Replace("v", "_|__||_|_|_||");
        strTmp = strTmp.Replace("w", "_||__||_|_|_|");
        strTmp = strTmp.Replace("x", "_|__|_||_|_||");
        strTmp = strTmp.Replace("y", "_||__|_||_|_|");
        strTmp = strTmp.Replace("z", "_|__||_||_|_|");
        strTmp = strTmp.Replace("-", "_|__|_|_||_||");
        strTmp = strTmp.Replace("*", "_|__|_||_||_|");
        strTmp = strTmp.Replace("/", "_|__|__|_|__|");
        strTmp = strTmp.Replace("%", "_|_|__|__|__|");
        strTmp = strTmp.Replace("+", "_|__|_|__|__|");
        strTmp = strTmp.Replace(".", "_||__|_|_||_|");
        strTmp = strTmp.Replace("_", "<span   style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
        strTmp = strTmp.Replace("|", "<span   style='height:" + height + ";width:" + width + ";background:#000000;'></span>");

        if (type_code == 1)
        {
            return strTmp + "<BR>" + code;
        }
        else
        {
            return strTmp;
        }
        #endregion
    }
       
    public static string ClearHtml(string HtmlString)
    {
        string pn = "(</?.*?/?>)";
        HtmlString = Regex.Replace(HtmlString, pn, "");
        return HtmlString;
    }

    public static string ClearFormat(string HtmlString)
    {
        HtmlString = HtmlString.Replace("\r\n", string.Empty).Replace(" ", string.Empty);
        return HtmlString.Trim();
    }

    #region 獲取N行數據
    /// <summary>
    /// 取指定行數數據
    /// </summary>
    /// <param name="str">傳入的待取字符串</param>
    /// <param name="rowsnum">指定的行數</param>
    /// <param name="strnum">每行的英文字符數或字節數</param>
    /// <returns></returns>
    public static string GetContent(string str, int rowsnum, int strnum)
    {
        //1計算內容塊           
        string content = str.Replace("\r\n", "§");
        string[] strContent = content.Split(Convert.ToChar("§"));

        int strCount = rowsnum * strnum;
        int cutrow = rowsnum - strContent.Length;
        cutrow = rowsnum > 10 ? rowsnum : 10;
        int pStrCount;
        string setOkStr = "";


        //2對內容塊進行
        for (int i = 0; i < strContent.Length; i++)
        {
            pStrCount = System.Text.Encoding.Default.GetBytes(strContent[i]).Length;
            if (pStrCount < strCount)
            {
                setOkStr += strContent[i] + "<br>";
                rowsnum -= Convert.ToInt32(Math.Ceiling((double)pStrCount / (double)strnum));
                strCount = rowsnum * strnum;
            }
            else
            {
                if (rowsnum > 0)
                {
                    setOkStr += CutStr(strContent[i], rowsnum * strnum, cutrow);
                }
                else
                {
                    //減去rowsnum是為了避免有些行長度為90,有的為89的現像
                    setOkStr = setOkStr.Substring(0, setOkStr.Length - cutrow / 2) + "...";
                }
                break;
            }
        }

        setOkStr = setOkStr.Replace("  ", " "); //軟(半角)空格轉硬(全角)空格
        return setOkStr;

    }

    //字符串截取函數
    public static string CutStr(string str, int length, int rowsnum)
    {
        if (System.Text.Encoding.Default.GetBytes(str).Length < length)
            return str;

        length = length - rowsnum;
        int i = 0, j = 0;
        foreach (char chr in str)
        {
            if ((int)chr > 127)
                i += 2;
            else
                i++;
            if (i > length)
            {
                str = str.Substring(0, j) + "...";
                break;
            }
            j++;
        }
        return str;

    }
    #endregion

    #region 得到整型
    public static int ConvertToInt(string Str)
    {
        return Str.Trim() == string.Empty ? 0 : int.Parse(Str);
    }

    public static int GetInt(object o)
    {
        #region
        if (o == DBNull.Value || o == null)
            return 0;
        else
            return Convert.ToInt32(o);
        #endregion
    } 
    #endregion

    /// <summary>
    /// 轉化成時間類型
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static object ConvertDate(object o)
    {
        DateTime dt;
        if(DateTime.TryParse(o.ToString(),out dt))
            return dt;
        else
            return DBNull.Value;
    }

    #region 閉合HTML代碼
    public static string CloseHTML(string str)
    {
        string[] HtmlTag = new string[] { "p", "div", "span", "table", "ul", "font", "b", "u", "i", "a", "h1", "h2", "h3", "h4", "h5", "h6" };

        for (int i = 0; i < HtmlTag.Length; i++)
        {
            int OpenNum = 0, CloseNum = 0;
            Regex re = new Regex("<" + HtmlTag + "[^>]*" + ">", RegexOptions.IgnoreCase);
            MatchCollection m = re.Matches(str);
            OpenNum = m.Count;
            re = new Regex("</" + HtmlTag + ">", RegexOptions.IgnoreCase);
            m = re.Matches(str);
            CloseNum = m.Count;

            for (int j = 0; j < OpenNum - CloseNum; j++)
            {
                str += "</" + HtmlTag + ">";
            }
        }

        return str;
    } 
    #endregion

    /// <summary>
    /// 得到192.248.23.*的IP
    /// </summary>
    /// <param name="Str">IP地址</param>
    /// <returns></returns>
    public static string GetSortIp(string Str)
    {
        int x = Str.LastIndexOf('.')-1;
        return Str.Substring(0, x) + "*.*";
    }

    /// <summary>
    /// 獲取年月
    /// </summary>
    /// <returns></returns>
    public static string GetYearMonth()
    {
        return DateTime.Now.ToString("yyyyMM") ;
    }

    #region 獲取遠程頁面內容
    public static string GetHttpData(string Url)
    {
        //string sException = null;
        string sRslt = null;
        WebResponse oWebRps = null;
        WebRequest oWebRqst = WebRequest.Create(Url);
        oWebRqst.Timeout = 50000;
        try
        {
            oWebRps = oWebRqst.GetResponse();
        }
        catch (WebException e)
        {
            //sException = e.Message.ToString();
            //Response.Write(sException);
        }
        catch (Exception e)
        {
            //sException = e.ToString();
            //Response.Write(sException);
        }
        finally
        {
            if (oWebRps != null)
            {
                var oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("UTF-8"));//GB2312|UTF-8"
                sRslt = oStreamRd.ReadToEnd();
                oStreamRd.Close();
                oWebRps.Close();
            }
        }
        return sRslt;
    }

    public string[] GetData(string Html)
    {
        String[] rS = new String[2];
        string s = Html;
        s = Regex.Replace(s, "\\s{3,}", "");
        s = s.Replace("\r", "");
        s = s.Replace("\n", "");
        string Pat = "<td align=\"center\" class=\"24p\"><B>(.*)</B></td></tr><tr>.*(<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">.*</table>)<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">(.*)<td align=center class=l6h>";
        Regex Re = new Regex(Pat);
        Match Ma = Re.Match(s);
        if (Ma.Success)
        {
            rS[0] = Ma.Groups[1].ToString();
            rS[1] = Ma.Groups[2].ToString();
            //pgStr = Ma.Groups[3].ToString();
        }
        return rS;
    } 
    #endregion

    /// <summary>
    /// 判斷頁面是否存在
    /// </summary>
    /// <param name="sURL"></param>
    /// <returns></returns>
    public static bool UrlExist(string sURL)
    {
        #region 
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
            //WebProxy   proxy   =   new   WebProxy("your   proxy   server",   8080);   
            //request.Proxy   =   proxy;   
            request.Method = "HEAD";
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            bool result = false;
            switch (response.StatusCode)
            {
                case HttpStatusCode.OK:
                    result = true;
                    break;
                case HttpStatusCode.Moved:
                    break;
                case HttpStatusCode.NotFound:
                    break;
            }
            response.Close();
            return result;
        }
        catch
        {
            return false;
        } 
        #endregion
    }

    #region 獲取字串中的鏈接
    /// <summary>
    /// 獲取字串中的鏈接
    /// </summary>
    /// <param name="HtmlCode"></param>
    /// <returns></returns>
    public static ArrayList GetPageUrl(string HtmlCode)
    {
        ArrayList my_list = new ArrayList();
        string p = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
        Regex re = new Regex(p, RegexOptions.IgnoreCase);
        MatchCollection mc = re.Matches(HtmlCode);

        for (int i = 0; i <= mc.Count - 1; i++)
        {
            string name = mc[i].ToString();
            if (!my_list.Contains(name))//排除重復URL
            {
                my_list.Add(name);
            }
        }
        return my_list;
    } 
    #endregion


    /// <summary>
    /// 將 Stream 轉化成 string
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string ConvertStreamToString(Stream s)
    {
        #region 
        string strResult = "";
        StreamReader sr = new StreamReader(s, Encoding.UTF8);

        Char[] read = new Char[256];

        // Read 256 charcters at a time.    
        int count = sr.Read(read, 0, 256);

        while (count > 0)
        {
            // Dump the 256 characters on a string and display the string onto the console.
            string str = new String(read, 0, count);
            strResult += str;
            count = sr.Read(read, 0, 256);
        }


        // 釋放資源
        sr.Close();

        return strResult; 
        #endregion
    }

    /// <summary>
    /// 對傳遞的參數字符串進行處理,防止注入式攻擊
    /// </summary>
    /// <param name="str">傳遞的參數字符串</param>
    /// <returns>String</returns>
    public static string ConvertSql(string str)
    {
        #region 
        str = str.Trim();
        str = str.Replace("'", "''");
        str = str.Replace(";--", "");
        str = str.Replace("=", "");
        str = str.Replace(" or ", "");
        str = str.Replace(" and ", "");

        return str; 
        #endregion
    }

    /// <summary>
    /// 格式化占用空間大小的輸出
    /// </summary>
    /// <param name="size">大小</param>
    /// <returns>返回 String</returns>
    public static string FormatNUM(long size)
    {
        #region 
        decimal NUM;
        string strResult;

        if (size > 1073741824)
        {
            NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1073741824));
            strResult = NUM.ToString("N") + " M";
        }
        else if (size > 1048576)
        {
            NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1048576));
            strResult = NUM.ToString("N") + " M";
        }
        else if (size > 1024)
        {
            NUM = (Convert.ToDecimal(size) / Convert.ToDecimal(1024));
            strResult = NUM.ToString("N") + " KB";
        }
        else
        {
            strResult = size + " 字節";
        }

        return strResult; 
        #endregion
    }

    /// <summary>
    /// 獲取數組元素的合並字符串
    /// </summary>
    /// <param name="stringArray"></param>
    /// <returns></returns>
    public static string GetArrayString(string[] stringArray)
    {
        #region 
        string totalString = null;
        for (int i = 0; i < stringArray.Length; i++)
        {
            totalString = totalString + stringArray[i];
        }
        return totalString; 
        #endregion
    }

    /// <summary>
    /// 將指定字符串中的漢字轉換為拼音首字母的縮寫,其中非漢字保留為原字符
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string ConvertSpellFirst(string text)
    {
        #region

        var sb = new StringBuilder(text.Length);
        foreach (char c in text)
        {
            var pinyin = c;
            byte[] array = Encoding.Default.GetBytes(new[] { c });

            if (array.Length == 2)
            {
                var i = array[0] * 0x100 + array[1];

                #region 條件匹配
                if (i < 0xB0A1) pinyin = c;
                else
                    if (i < 0xB0C5) pinyin = 'a';
                    else
                        if (i < 0xB2C1) pinyin = 'b';
                        else
                            if (i < 0xB4EE) pinyin = 'c';
                            else
                                if (i < 0xB6EA) pinyin = 'd';
                                else
                                    if (i < 0xB7A2) pinyin = 'e';
                                    else
                                        if (i < 0xB8C1) pinyin = 'f';
                                        else
                                            if (i < 0xB9FE) pinyin = 'g';
                                            else
                                                if (i < 0xBBF7) pinyin = 'h';
                                                else
                                                    if (i < 0xBFA6) pinyin = 'g';
                                                    else
                                                        if (i < 0xC0AC) pinyin = 'k';
                                                        else
                                                            if (i < 0xC2E8) pinyin = 'l';
                                                            else
                                                                if (i < 0xC4C3) pinyin = 'm';
                                                                else
                                                                    if (i < 0xC5B6) pinyin = 'n';
                                                                    else
                                                                        if (i < 0xC5BE) pinyin = 'o';
                                                                        else
                                                                            if (i < 0xC6DA) pinyin = 'p';
                                                                            else
                                                                                if (i < 0xC8BB) pinyin = 'q';
                                                                                else
                                                                                    if (i < 0xC8F6) pinyin = 'r';
                                                                                    else
                                                                                        if (i < 0xCBFA) pinyin = 's';
                                                                                        else
                                                                                            if (i < 0xCDDA) pinyin = 't';
                                                                                            else
                                                                                                if (i < 0xCEF4) pinyin = 'w';
                                                                                                else
                                                                                                    if (i < 0xD1B9) pinyin = 'x';
                                                                                                    else
                                                                                                        if (i < 0xD4D1) pinyin = 'y';
                                                                                                        else
                                                                                                            if (i < 0xD7FA) pinyin = 'z';
                #endregion
            }

            sb.Append(pinyin);
        }           

        return sb.ToString();
        #endregion
    }

    #region Request
    /// <summary></summary>
    /// <param name="filedName"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static int Request(string filedName, int defaultValue)
    {
        return Request(filedName, defaultValue, false);
    }

    /// <summary>
    /// 獲取URL指定參數,如果參數不存在或者為空就返回默認值
    /// </summary>
    /// <param name="filedName">請求KEY</param>
    /// <param name="defaultValue">默認值</param>
    /// <param name="isDecrypt">是否需要解密</param>
    /// <returns></returns>
    public static int Request(string filedName, int defaultValue, bool isDecrypt)
    {
        string str = HttpContext.Current.Request[filedName];
        if (str != null && str.Trim() != "")
        {
            if (isDecrypt)
            {
                str = new  PblogDes().Decrypt(str);
            }
            return int.Parse(str);
        }

        return defaultValue;
    }

    /// <summary>
    /// 獲取URL指定參數,如果參數不存在或者為空就拋出錯誤信息
    /// </summary>
    /// <param name="filedName"></param>
    /// <returns></returns>
    public static int Request(string filedName)
    {
        return Request(filedName, false);
    }

    /// <summary>
    /// 獲取URL指定參數,如果參數不存在或者為空就拋出錯誤信息
    /// </summary>
    /// <param name="filedName"></param>
    /// <param name="isDecrypt">是否需要解密</param>
    /// <returns></returns>
    public static int Request(string filedName, bool isDecrypt)
    {
        int i = Request(filedName, -1, isDecrypt);
        if (i == -1)
        {
            throw new Exception("找不到指定參數:" + filedName);
        }

        return i;
    }
    #endregion
}

 

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