-
C#特殊字符处理
- <span style="font-size:14px;"><strong>private static Regex RegNumber = new Regex("^[0-9]+$");
- private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
- private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
- private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
- private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
- private static Regex RegCHZN = new Regex("[\一-\龥]");
-
- #region//对入库字符进行编码和转换。或用Server.HtmlEncode(enstr)
- public static string EncodeStr(string str)
- {
- str=str.Replace("'","’");
- str=str.Replace("\"",""");
- str=str.Replace("<","<");
- str=str.Replace(">",">");
- str=str.Replace("\n","<br>");
- return str;
- }
- #endregion
-
- #region//对出库字符进入显示时的转换。或用Server.HtmlDecode(str)
- public static string DecodeStr(string str)
- {
- str=str.Replace("’","'");
- str=str.Replace(""","\"");
- str=str.Replace("<","<");
- str=str.Replace(">",">");
- str=str.Replace("<br>","\n");
- return str;
- }
- #endregion
-
- 数字字符串检查
-
-
-
-
-
- public static bool IsNumber(string inputData)
- {
- Match m = RegNumber.Match(inputData);
- return m.Success;
- }
-
-
-
-
-
- public static bool IsNumberSign(string inputData)
- {
- Match m = RegNumberSign.Match(inputData);
- return m.Success;
- }
-
-
-
-
-
- public static bool IsDecimal(string inputData)
- {
- Match m = RegDecimal.Match(inputData);
- return m.Success;
- }
-
-
-
-
-
- public static bool IsDecimalSign(string inputData)
- {
- Match m = RegDecimalSign.Match(inputData);
- return m.Success;
- }
-
-
-
-
-
-
-
- public static string ReplaceStr(string str)
- {
-
- str=str.Replace("'","");
- str=str.Replace(";","");
- str=str.Replace(":","");
- str=str.Replace("/","");
- str=str.Replace("?","");
- str=str.Replace("<","");
- str=str.Replace(">","");
- str=str.Replace(".","");
- str=str.Replace("#","");
- str=str.Replace("%","");
- str=str.Replace("%","");
- str=str.Replace("^","");
- str=str.Replace("//","");
- str=str.Replace("@","");
- str=str.Replace("(","");
- str=str.Replace(")","");
- str=str.Replace("*","");
- str=str.Replace("~","");
- str=str.Replace("`","");
- str=str.Replace("$","");
-
- return str;
- }
-
-
-
- public static bool checkURl(string str)
- {
- Regex r=new Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
-
- Match m = r.Match(str);
- if(m.Success)
- {
- return true;
- }
- else
- {
- return false;
- }
-
- }
-
-
-
- public static bool checkDate(string str)
- {
-
- Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");
- Match m =r.Match(str);
-
- if(m.Success)
- {
- return true;
- }
- else
- {
- return false;
-
- }
-
- }
-
- public static bool checkInDate(string str)
- {
- Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");
- Match m =r.Match(str);
- if(m.Success)
- {
- string M=m.ToString();
- char [] da = new char[10];
-
- M.CopyTo(0,da,0,10);
-
- bool bl=false;
- string yy="";
- string mm="";
- string dd="";
-
- #region 分别取出 YYYY、MM、DD
- for(int i=0;i<4;i++)
- {
- yy +=da[i].ToString();
- }
-
- for(int i=0;i<2;i++)
- {
- mm+=da[5+i].ToString();
- }
-
- for(int i=0;i<2;i++)
- {
- dd+=da[8+i].ToString();
- }
- #endregion
-
- int YearLow=int.Parse( ConfigurationSettings.AppSettings["YearLow"]);
- int YearHigh=int.Parse( ConfigurationSettings.AppSettings["YearHigh"]);
- if(int.Parse(yy)<YearHigh && int.Parse(yy)>YearLow)
- {
- #region 是否为有效的日期值
- bool b=true;
- if(b)
- {
- bool B=false;
- switch(mm)
- {
- case "01": B=true;
- break;
- case "02": B=true;
- break;
- case "03": B=true;
- break;
- case "04": B=true;
- break;
- case "05": B=true;
- break;
- case "06": B=true;
- break;
- case "07": B=true;
- break;
- case "08": B=true;
- break;
- case "09": B=true;
- break;
- case "10": B=true;
- break;
- case "11": B=true;
- break;
- case "12": B=true;
- break;
- }
- if(B)
- {
- switch(dd)
- {
- case "01": bl=true;
- break;
- case "02": bl=true;
- break;
- case "03": bl=true;
- break;
- case "04": bl=true;
- break;
- case "05": bl=true;
- break;
- case "06": bl=true;
- break;
- case "07": bl=true;
- break;
- case "08": bl=true;
- break;
- case "09": bl=true;
- break;
- case "10": bl=true;
- break;
- case "11": bl=true;
- break;
- case "12": bl=true;
- break;
- case "13": bl=true;
- break;
- case "14": bl=true;
- break;
- case "15": bl=true;
- break;
- case "16": bl=true;
- break;
- case "17": bl=true;
- break;
- case "18": bl=true;
- break;
- case "19": bl=true;
- break;
- case "20": bl=true;
- break;
- case "21": bl=true;
- break;
- case "22": bl=true;
- break;
- case "23": bl=true;
- break;
- case "24": bl=true;
- break;
- case "25": bl=true;
- break;
- case "26": bl=true;
- break;
- case "27": bl=true;
- break;
- case "28": bl=true;
- break;
- case "29": bl=true;
- break;
- case "30": bl=true;
- break;
- case "31": bl=true;
- break;
- }
- }
- }
- #endregion
- }
- else
- {
- bl=false;
- }
-
- if(bl)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
-
- }</strong></span>
-
相关阅读:
IEnumerable、IEnumerator接口(如何增加迭代器功能)
IConvertible接口
IComparable和IComparer接口
c#-泛型、协变、逆变
MVC-HtmlHelper扩展
.Net反射-TypeDescriptor
MVC Core 使用TagHelper扩展几个插件
js日志组件封装
js预解析
js代码优化
-
原文地址:https://www.cnblogs.com/luckys/p/6697772.html
Copyright © 2020-2023
润新知