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]+$"); //等价于^[+-]?\d+[.]?\d+$
  private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 
  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

数字字符串检查 
  /// <summary>
  /// 是否数字字符串
  /// </summary>
  /// <param name="inputData">输入字符串</param>
  /// <returns></returns>
  public static bool IsNumber(string inputData)
  {
   Match m = RegNumber.Match(inputData);
   return m.Success;
  }  
  /// <summary>
  /// 是否数字字符串 可带正负号
  /// </summary>
  /// <param name="inputData">输入字符串</param>
  /// <returns></returns>
  public static bool IsNumberSign(string inputData)
  {
   Match m = RegNumberSign.Match(inputData);
   return m.Success;
  }  
  /// <summary>
  /// 是否是浮点数
  /// </summary>
  /// <param name="inputData">输入字符串</param>
  /// <returns></returns>
  public static bool IsDecimal(string inputData)
  {
   Match m = RegDecimal.Match(inputData);
   return m.Success;
  }  
  /// <summary>
  /// 是否是浮点数 可带正负号
  /// </summary>
  /// <param name="inputData">输入字符串</param>
  /// <returns></returns>
  public static bool IsDecimalSign(string inputData)
  {
   Match m = RegDecimalSign.Match(inputData);
   return m.Success;
  }  


 /// <summary>
  /// 非法字符转换
  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>
  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("`","");
   str=str.Replace("$","");

   return str;  
  }


  //检查url
  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];//char.Parse(m.ToString());
   
    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>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值