这是更快:DateTime.TryParse或正则表达式 [英] Which is Quicker: DateTime.TryParse or Regex

查看:301
本文介绍了这是更快:DateTime.TryParse或正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,以确定提供的字符串是否是一个日期,其中之一是更快:使用日期时间将其转换为一个日期,或使用常规的前pression检查字符串的有效性作为一个日期?

In .NET, to determine whether a supplied string is a date, which one is quicker: using DateTime to cast it to a date, or using a regular expression to check the string for its validity as a date?

我只需要确保提供的值是一个日期,而我没有做任何事情与它之后。

I just need to ensure the value supplied is a date, and I'm not doing anything with it afterward.

感谢。

推荐答案

一个良好的正则表达式要快很多,也许消耗更少的瞬时记忆。

A good regex should be much faster, and maybe consume less transient memory.

但这里的硬币的另一面:

But here's the flipside of the coin:

您是pretty的,很多依赖于只有一个时间格式,这意味着国际化将是痛苦的,而你的用户需要被教育的日期以正确的格式输入。

You're pretty-much tied to only one time format, which means that internationalization will be painful, and that your users need to be educated to enter the date in the proper format.

此外,你会失去一些最新的验证,也就是说,你怎么在非闰年年杂草出2月29日? 4月31日?

Also, you will lose some date validation, say, how do you weed-out Feb 29th on non leap-years? April 31st?

这篇关于这是更快:DateTime.TryParse或正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆