开发者

Check Format of a string

What is the sma开发者_JAVA技巧llest amount of C# possible to Check that a String fits this format #-##### (1 number, a dash then 5 more numbers).

It seems to me that a regular expression could do this quick (again, I wish I knew regular expressions).

So, here is an example:

public bool VerifyBoxNumber (string boxNumber)
{
   // psudo code
   if (boxNumber.FormatMatch("#-#####")
      return true;
   return false;
}

If you know real code that will make the above comparison work, please add an answer.


private static readonly Regex boxNumberRegex = new Regex(@"^\d-\d{5}$");

public static bool VerifyBoxNumber (string boxNumber)
{
   return boxNumberRegex.IsMatch(boxNumber);
}


return Regex.IsMatch(boxNumber, @"^\d-\d{5}$");


^\d-\d{5}$ would be a regexp that matches only this pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜