对字符串的综合处理的几个方法

                       这个几个方法就是对输入的字符串进行处理

第一个是防止sql注入式攻击的

            public static string ConvertSql(string str)
            {
                str = str.Trim();
                str = str.Replace("'", "''");
                str = str.Replace(";--", "");
                str = str.Replace("=", "");
                str = str.Replace(" or ", "");
                str = str.Replace(" and ", "");

                return str;
            }

第二个是计算占用的空间大小的

            /// <summary>
            /// 格式化占用空间大小的输出
            /// </summary>
            /// <param name="size">大小</param>
            /// <returns>返回 String</returns>
            public static string FormatNUM(long size)
            {
                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;
            }

另外给上格式说明:

格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化。我们使用格式字符串指定格式。格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数。

格式说明符
说明
示例
输出
C
货币
2.5.ToString("C")
¥2.50
D
十进制数
25.ToString("D5")
00025
E
科学型
25000.ToString("E")
2.500000E+005
F
固定点
25.ToString("F2")
25.00
G
常规
2.5.ToString("G")
2.5
N
数字
2500000.ToString("N")
2,500,000.00
X
十六进制
255.ToString("X")
FF
 

第三个是判断是否合格的电邮和地址url的

public static bool IsValidEmail(string email)
            {
                return Regex.IsMatch(email, @"^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$");
            }

public static bool IsValidURL(string url)
            {
                return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
            }

第四个是判断是否是int型,是否是数字,

/// <summary>
            /// 判断字符串是否为Int类型的
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static bool IsValidInt(string val)
            {
                return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
            }

 /// <summary>
            /// 检测字符串是否全为正整数
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsNum(string str)
            {
                bool blResult = true;//默认状态下是数字

                if (str == "")
                    blResult = false;
                else
                {
                    foreach (char Char in str)
                    {
                        if (!char.IsNumber(Char))
                        {
                            blResult = false;
                            break;
                        }
                    }
                    if (blResult)
                    {
                        if (int.Parse(str) == 0)
                            blResult = false;
                    }
                }
                return blResult;
            }

 /// <summary>
            /// 检测字符串是否全为数字型
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool IsDouble(string str)
            {
                bool blResult = true;//默认状态下是数字

                if (str == "")
                    blResult = false;
                else
                {
                    foreach (char Char in str)
                    {
                        if (!char.IsNumber(Char) && Char.ToString() != "-")
                        {
                            blResult = false;
                            break;
                        }
                    }
                }
                return blResult;
            }

还有其他的几个

 /// <summary>
            /// 返回字符串的真实长度,一个汉字字符相当于两个单位长度
            /// </summary>
            /// <param name="str">指定字符串</param>
            /// <returns></returns>
            public static int Len(string str)
            {
                int intResult = 0;

                foreach (char Char in str)
                {
                    if ((int)Char > 127)
                        intResult += 2;
                    else
                        intResult++;
                }
                return intResult;
            }

 

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

posted @ 2007-04-03 02:02  音乐啤酒  阅读(247)  评论(0编辑  收藏  举报