C#特殊字符处理

  1. <span style="font-size:14px;"><strong>private static Regex RegNumber = new Regex("^[0-9]+$");  
  2.   private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");  
  3.   private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");  
  4.   private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$  
  5.   private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样   
  6.   private static Regex RegCHZN = new Regex("[\一-\龥]");  
  7.  
  8.   #region//对入库字符进行编码和转换。或用Server.HtmlEncode(enstr)  
  9.   public static string EncodeStr(string str)  
  10.   {  
  11.    str=str.Replace("'","’");  
  12.    str=str.Replace("\"",""");  
  13.    str=str.Replace("<","<");  
  14.    str=str.Replace(">",">");  
  15.    str=str.Replace("\n","<br>");  
  16.    return str;  
  17.   }  
  18.         #endregion  
  19.  
  20.   #region//对出库字符进入显示时的转换。或用Server.HtmlDecode(str)  
  21.   public static string DecodeStr(string str)  
  22.   {  
  23.    str=str.Replace("’","'");  
  24.    str=str.Replace(""","\"");  
  25.    str=str.Replace("<","<");  
  26.    str=str.Replace(">",">");  
  27.    str=str.Replace("<br>","\n");  
  28.    return str;  
  29.   }  
  30.         #endregion  
  31.   
  32. 数字字符串检查   
  33.   /// <summary>  
  34.   /// 是否数字字符串  
  35.   /// </summary>  
  36.   /// <param name="inputData">输入字符串</param>  
  37.   /// <returns></returns>  
  38.   public static bool IsNumber(string inputData)  
  39.   {  
  40.    Match m = RegNumber.Match(inputData);  
  41.    return m.Success;  
  42.   }    
  43.   /// <summary>  
  44.   /// 是否数字字符串 可带正负号  
  45.   /// </summary>  
  46.   /// <param name="inputData">输入字符串</param>  
  47.   /// <returns></returns>  
  48.   public static bool IsNumberSign(string inputData)  
  49.   {  
  50.    Match m = RegNumberSign.Match(inputData);  
  51.    return m.Success;  
  52.   }    
  53.   /// <summary>  
  54.   /// 是否是浮点数  
  55.   /// </summary>  
  56.   /// <param name="inputData">输入字符串</param>  
  57.   /// <returns></returns>  
  58.   public static bool IsDecimal(string inputData)  
  59.   {  
  60.    Match m = RegDecimal.Match(inputData);  
  61.    return m.Success;  
  62.   }    
  63.   /// <summary>  
  64.   /// 是否是浮点数 可带正负号  
  65.   /// </summary>  
  66.   /// <param name="inputData">输入字符串</param>  
  67.   /// <returns></returns>  
  68.   public static bool IsDecimalSign(string inputData)  
  69.   {  
  70.    Match m = RegDecimalSign.Match(inputData);  
  71.    return m.Success;  
  72.   }    
  73.   
  74.   
  75.  /// <summary>  
  76.   /// 非法字符转换  
  77.   /// </summary>  
  78.   /// <param name="str"></param>  
  79.   /// <returns></returns>  
  80.   public static string ReplaceStr(string str)  
  81.   {  
  82.    //str=str.Replace(",","");  
  83.    str=str.Replace("'","");  
  84.    str=str.Replace(";","");  
  85.    str=str.Replace(":","");  
  86.    str=str.Replace("/","");  
  87.    str=str.Replace("?","");  
  88.    str=str.Replace("<","");  
  89.    str=str.Replace(">","");  
  90.    str=str.Replace(".","");  
  91.    str=str.Replace("#","");  
  92.    str=str.Replace("%","");  
  93.    str=str.Replace("%","");  
  94.    str=str.Replace("^","");  
  95.    str=str.Replace("//","");  
  96.    str=str.Replace("@","");  
  97.    str=str.Replace("(","");  
  98.    str=str.Replace(")","");  
  99.    str=str.Replace("*","");  
  100.    str=str.Replace("~","");  
  101.    str=str.Replace("`","");  
  102.    str=str.Replace("$","");  
  103.   
  104.    return str;    
  105.   }  
  106.   
  107.   
  108.   //检查url  
  109.   public static bool checkURl(string str)  
  110.   {  
  111.      Regex r=new Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");  
  112.       
  113.    Match m = r.Match(str);  
  114.    if(m.Success)  
  115.    {  
  116.     return true;  
  117.    }  
  118.    else  
  119.    {  
  120.     return false;  
  121.    }  
  122.   
  123.   }  
  124.   
  125.     
  126.   
  127. //日期  
  128.   public static bool checkDate(string str)  
  129.   {  
  130.   
  131.       Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");  
  132.    Match m =r.Match(str);  
  133.                
  134.    if(m.Success)  
  135.    {  
  136.     return true;  
  137.    }  
  138.    else  
  139.    {  
  140.        return false;  
  141.      
  142.    }  
  143.   
  144.   }  
  145.   
  146.   public static bool checkInDate(string str)  
  147.   {  
  148.       Regex r = new Regex("^(\\d{4})\\-(\\d{2})\\-(\\d{2})$");  
  149.    Match m =r.Match(str);  
  150.         if(m.Success)  //格式正确  
  151.    {  
  152.     string M=m.ToString();  
  153.     char [] da = new char[10];//char.Parse(m.ToString());  
  154.      
  155.     M.CopyTo(0,da,0,10);  
  156.   
  157.     bool bl=false;  
  158.     string yy="";  
  159.     string mm="";  
  160.     string dd="";  
  161.  
  162.    #region 分别取出 YYYY、MM、DD  
  163.     for(int i=0;i<4;i++)  
  164.     {  
  165.      yy +=da[i].ToString();  
  166.     }  
  167.       
  168.     for(int i=0;i<2;i++)  
  169.     {  
  170.      mm+=da[5+i].ToString();  
  171.     }  
  172.   
  173.     for(int i=0;i<2;i++)  
  174.     {  
  175.      dd+=da[8+i].ToString();  
  176.     }  
  177.    #endregion  
  178.   
  179.     int YearLow=int.Parse( ConfigurationSettings.AppSettings["YearLow"]);  
  180.     int YearHigh=int.Parse( ConfigurationSettings.AppSettings["YearHigh"]);  
  181.     if(int.Parse(yy)<YearHigh && int.Parse(yy)>YearLow)  //  
  182.     {  
  183.      #region  是否为有效的日期值  
  184.      bool b=true;  
  185.      if(b)  
  186.      {  
  187.       bool B=false;  
  188.       switch(mm)  
  189.       {  
  190.        case "01": B=true;  
  191.         break;  
  192.        case "02": B=true;  
  193.         break;  
  194.        case "03": B=true;  
  195.         break;  
  196.        case "04": B=true;  
  197.         break;  
  198.        case "05": B=true;  
  199.         break;  
  200.        case "06": B=true;  
  201.         break;  
  202.        case "07": B=true;  
  203.         break;  
  204.        case "08": B=true;  
  205.         break;  
  206.        case "09": B=true;  
  207.         break;  
  208.        case "10": B=true;  
  209.         break;  
  210.        case "11": B=true;  
  211.         break;  
  212.        case "12": B=true;  
  213.         break;  
  214.       }  
  215.       if(B)  
  216.       {  
  217.        switch(dd)  
  218.        {  
  219.         case "01": bl=true;  
  220.          break;  
  221.         case "02": bl=true;  
  222.          break;  
  223.         case "03": bl=true;  
  224.          break;  
  225.         case "04": bl=true;  
  226.          break;  
  227.         case "05": bl=true;  
  228.          break;  
  229.         case "06": bl=true;  
  230.          break;  
  231.         case "07": bl=true;  
  232.          break;  
  233.         case "08": bl=true;  
  234.          break;  
  235.         case "09": bl=true;  
  236.          break;  
  237.         case "10": bl=true;  
  238.          break;  
  239.         case "11": bl=true;  
  240.          break;  
  241.         case "12": bl=true;  
  242.          break;  
  243.         case "13": bl=true;  
  244.          break;  
  245.         case "14": bl=true;  
  246.          break;  
  247.         case "15": bl=true;  
  248.          break;  
  249.         case "16": bl=true;  
  250.          break;  
  251.         case "17": bl=true;  
  252.          break;  
  253.         case "18": bl=true;  
  254.          break;  
  255.         case "19": bl=true;  
  256.          break;  
  257.         case "20": bl=true;  
  258.          break;  
  259.         case "21": bl=true;  
  260.          break;  
  261.         case "22": bl=true;  
  262.          break;  
  263.         case "23": bl=true;  
  264.          break;  
  265.         case "24": bl=true;  
  266.          break;  
  267.         case "25": bl=true;  
  268.          break;  
  269.         case "26": bl=true;  
  270.          break;  
  271.         case "27": bl=true;  
  272.          break;  
  273.         case "28": bl=true;  
  274.          break;  
  275.         case "29": bl=true;  
  276.          break;  
  277.         case "30": bl=true;  
  278.          break;  
  279.         case "31": bl=true;  
  280.          break;  
  281.        }  
  282.       }  
  283.      }  
  284.      #endregion  
  285.     }  
  286.     else  
  287.     {  
  288.      bl=false;  
  289.     }  
  290.                
  291.     if(bl)  
  292.     {  
  293.      return true;  
  294.     }  
  295.     else  
  296.     {  
  297.      return false;  
  298.     }  
  299.    }  
  300.    else  
  301.    {  
  302.      return false;  
  303.    }  
  304.     
  305.   }</strong></span>  

 

原文地址:https://www.cnblogs.com/luckys/p/6697772.html