【发布时间】:2013-06-06 10:23:22
【问题描述】:
我需要在字符串中搜索和解析日期时间(根据当前文化格式化)。
目前我能想到的最好的是:
string text = String.Format("Time is {0}; all's well", DateTime.Now);
DateTime date = new DateTime();
for (int start = 0; start < text.Length - 1; start++)
for (int length = text.Length - start; length > 0; length--)
if (DateTime.TryParse(text.Substring(start, length), out date))
return date;
有没有更聪明的方法来做到这一点?
【问题讨论】:
-
您对要在其中查找的字符串了解更多信息吗?还是应该是任意字符串?
-
如果不了解字符串的结构以及日期时间在其中的显示方式,我们就没有什么可做的了。你的意思是总是作为例子(“时间是......”)?