【问题标题】:How can we validate a string variable for numeric format?我们如何验证数字格式的字符串变量?
【发布时间】:2015-09-30 10:30:40
【问题描述】:

if 条件仅检查 null 或空,这不会修复缺陷。 我想检查Year字符串是否包含数字。

string Year = Request.Params[""year""];
if (Year == null || Year.Equals(""""))    
{    
    Year = System.DateTime.Now.Year.ToString();    
}

【问题讨论】:

标签: asp.net .net


【解决方案1】:

使用 TryParse:

int x;
if (Year == null || Year.Equals("""") || !int.TryParse(Year, out x)){
     // your code

}

【讨论】:

    【解决方案2】:

    试试下面的代码。它将检查字符串是否包含数值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多