C# - 如何检查字符串是否包含日期 [英] C#-How to check if a string contains a date

查看:339
本文介绍了C# - 如何检查字符串是否包含日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,用户可以在其中输入任何内容。



我需要检查输入的文本是否包含类似
$ b的日期$ b

如果用户输入我想要这个项目2011年11月21日



或10/15/2011在这两种情况下我都想要其他假的。



请建议

I have a text box where a user may enter any thing.

I need to check if the text entered contains a date like

if user is entering "I want this item by 11/21/2011"

or "10/15/2011" in both cases i want true else false.

Please suggest

推荐答案

这比它看起来 - 你必须应对任何日期格式 - 我可以想到三个可能让你困惑:

That is a lot harder than it looks - you have to presumably cope with any date format - I can think of three which could confuse you:
yyyy/MM/dd
dd/mm/yy
mm/dd/yy



如果他输入11/12/13这一年?这个月是哪一天?

如果他进入11/12/2013​​是12月11日或11月12日?

如果他输入1/2,那该怎么办?一半,或明年2月1日?

如果他进入2011年12月12日怎么办?



如果可以,请尝试更改你的用户界面这些日期是在日历控件中输入的 - 它对你来说要容易得多,对用户也可以更加清晰。


If he enters "11/12/13" which is the year? Which is the month?
If he enters "11/12/2013" is that 11th Dec or 12th Nov?
What if he enters 1/2 - is that a half, or 1st Feb next year?
What if he enters Dec 12 2011?

If you can, try to change your UI so that dates are entered in a calendar control instead - it is a lot easier for you, and could be a lot clearer for the user too.


你可以先使用例如String.Split [ ^ ]使用空格作为分隔符和数组中的每个项目将文本分成多个部分例如,使用 TryParse [ ^ ]。



然而,正如OriginalGriff写的那样,你会遇到类似日期和月份的不同日期格式的问题,你应该知道用户使用的格式,或打破如OriginalGriff建议的那样输入几个控件。
You could first use for example String.Split[^] to break the text into pieces using whitespace as a separator and the for each item in the array use for example TryParse[^].

However as OriginalGriff wrote, you will have problems regarding to similar dates and months in different date formats sou you should somehow know the format the user uses, or to break the input to several controls as OriginalGriff suggested.


我认为Mika Wendelius有想法解决这种情况,下面我将尝试将其放入代码中:

I think Mika Wendelius has the idea to resolve such a scenario and below i will attempt to put it in code:
Boolean hasDate = false;
DateTime dateTime = new DateTime();
String[] inputText = txtWords.Text().Split( " " );//split on a whitespace

foreach( String text in inputText )
{
   //Use the Parse() method
   try
   {
      dateTime = DateTime.Parse( text );
      hasDate = true;
      break;//no need to execute/loop further if you have your date
   }
   catch( Exception ex )
   {
      
   }
}

//after breaking from the foreach loop, you can check if hasDate=true
//if it is, then your user entered a date and you can retrieve it from the dateTime 

if( hasDate )
{
    //user entered a date, get it from dateTime
}
else
{
   //user didn't enter any date
}





使用 Dat eTime.Parse(),你不需要事先知道日期格式,如果不允许日期格式, DateTime.Parse()方法将无法解析当前的文本,进入catch,然后继续返回 foreach循环



希望能给你一个快速启动,



快乐编码,

Morgs



By using DateTime.Parse(), you do not need to know the date format before hand, if the date format is not allowed, the DateTime.Parse() method will simply fail to parse the current "text", go into the catch, and the continue back into then foreach loop!

Hope that gives you a jump start,

Happy coding,
Morgs

这篇关于C# - 如何检查字符串是否包含日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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