常见的字符串处理大全

1.判断字符串中是否有汉字

<html>
<head><title></title>
<script type="text/JavaScript">

function checkChinese()
{
 var str=document.getElementById("txtTest").value;
 var reg = /^[\u4e00-\u9fa5]+$/i;
 if (reg.test(str)){
  alert("文本框中都是中文"); 
 }
 else{
  alert("文本框中有除中文外的字符"); 
 }
}
</script>
</head>
<body >
 <center>
 请输入字符:<input name="txtTest" /><br>
<input type="button" name="btmTest" value="检测中文" onclick="checkChinese();" />
</center>
</body>
</html>

2 对字串进行 html标签的过滤.

 public static string SiftHtml(string strSrc)
        {
            int[] arrLeft = new int[strSrc.Length];
            int l = 0;

            for (int i = 0; i < strSrc.Length; i++)
            {
                if (strSrc[i] == '<')
                {
                    arrLeft[l++] = i;
                }
                if (strSrc[i] == '>' && l > 0)
                {
                    string strHead = strSrc.Substring(0, arrLeft[l - 1]);
                    string strFoot = strSrc.Substring(i + 1, strSrc.Length - i - 1);
                    strSrc = strHead + strFoot;
                    i -= i - arrLeft[l - 1] + 1;
                    l--;
                    if (l < 0)
                    {
                        l = 0;
                    }
                }
            }

            return strSrc;
        }

 

3.判断一字串是否为数字.

 public static bool IsNum(string strSrc)
        {
            string strReg = "^\\d+$";
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(strReg, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match m = r.Match(strSrc);

            if (m.Success)
            {
                return true;
            }
            return false;
        }

4.转化首字大写,其余小写。

  public static string ToFirstUpper(string str, bool onlyFirstWord)
        {
            if (str == null) return "";
            string str2 = "";
            str = str.ToLower();
            bool hasUpper = false;
            for (int i = 0; i < str.Length; i++)
            {
                char ch = str[i];
                if (Char.IsLetter(ch))
                {
                    if (i == 0 || (!Char.IsLetter(str[i - 1]) && (!hasUpper || !onlyFirstWord)))
                    {
                        ch = Char.ToUpper(ch);
                        hasUpper = true;
                    }
                }
                str2 += ch;
            }
            return str2;
        }

5.字符串替换

  public static string StringReplace(string str, char oldValue, char newValue)
        {
            str = str.Trim();
            if (str == null || str == string.Empty) return string.Empty;

            string[] tmpStr = str.Split(oldValue);
            int tmpCount = tmpStr.Length;
            if (tmpCount <= 1) return str;
            string rtnStr = string.Empty;
            for (int i = 0; i < tmpCount; i++)
            {
                if (tmpStr[i].Trim() != string.Empty)
                {
                    if (rtnStr != string.Empty)
                    {
                        rtnStr = rtnStr + newValue.ToString() + tmpStr[i];
                    }
                    else
                    {
                        rtnStr += tmpStr[i];
                    }
                }
            }

            return rtnStr;
        }

6. 本函数用于字符串数据入库前的安全检查,非法字符会被替换。

 public static string GetSafeString(string strInput, bool AddQuote)
        {
            if (strInput == null) return "";
            string strTemp = strInput;
            if (strInput != null && strInput != "")
            {

                strTemp = strTemp.Replace("{", "");
                strTemp = strTemp.Replace("}", "");
                //********************************

                strTemp = strTemp.Replace("'", "''");
                //strTemp = strTemp.Replace("&","");

                strTemp = strTemp.Replace(@"""", "");

                strTemp = strTemp.Replace("%", "%");

                strTemp = strTemp.Replace(";", ";");

                strTemp = strTemp.Replace("<", "&lt;");

                strTemp = strTemp.Replace(">", "&gt;");

                strTemp = strTemp.Replace("(", "(");

                strTemp = strTemp.Replace(")", ")");

                strTemp = strTemp.Replace("+", "+");

                //strTemp = strTemp.Replace("--","");

                strTemp = strTemp.Replace("/*", "");

 

                strTemp = strTemp.Replace("*/", "");

                strTemp = strTemp.Replace("delete", "");

                strTemp = strTemp.Replace("drop", "");

                strTemp = strTemp.Replace("insert", "");

                strTemp = strTemp.Replace("select", "");

                strTemp = strTemp.Replace("update", "");

                strTemp = strTemp.Replace("grant", "");

                strTemp = strTemp.Replace("alter", "");

                strTemp = strTemp.Replace("|", "");
                strTemp = strTemp.Replace("0x", "");
            }
            else
            {
                strTemp = "";
            }
            if (AddQuote)
            {
                strTemp = "'" + strTemp + "'";
            }
            return strTemp;
        }

7. 获取IP

        public static string GetIP()
        {
            string ip;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return ip;

        }

8.日期格式处理
        public static string GetDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

9.Html 加解密
        public static string HtmlDecode(string instr)
        {
            return System.Web.HttpContext.Current.Server.HtmlDecode(instr);

        }

        public static string HtmlEncode(string instr)
        {
            return System.Web.HttpContext.Current.Server.HtmlEncode(instr);

        }

10.获得主机

 public static string GetHost()
        {
            return System.Web.HttpContext.Current.Request.ServerVariables["Http_Host"].ToString();

        }

11.生成随机数字字符串

     public static string GetRandomCodeNum(int codeCount)
        {
            int rep = 0;

            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }

  生成随机字母字符串(数字字母混和)

public static string GetRandomCodeString(int codeCount)
        {
            int rep = 0;
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }

12. 截取指定长度字符串

 

  public static string GetSubString(string instr, int len)
        {
            if (string.IsNullOrEmpty(instr)) return "";

            if (instr.Length > len)
                return instr.Substring(0, len);
            else
                return instr;
        }

 


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值