找到给定字符串C *中的ddmmyyyy [英] Find the ddmmyyyy in the given string C*

查看:101
本文介绍了找到给定字符串C *中的ddmmyyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我有这样的字符串Welcome20032014totheworld2014。在这里我需要输出日期如下



dd:20

mm:03

yyyy:2014。



我需要C#中的代码。请期待你的帮助。



谢谢

Dhanasekaran Murugesan

Hi,


I have string like this "Welcome20032014totheworld2014" . Here I need the output date as below

dd : 20
mm : 03
yyyy : 2014.

I need the code in C#. Please look forward your help.

Thanks
Dhanasekaran Murugesan

推荐答案

你应该使用正则表达式。这是一个链接 http://www.regular-expressions.info/ [ ^ ]



正则表达式应为:



You should use regular expression. Here is a link http://www.regular-expressions.info/[^]

The regular expression should be:

const string expresion = @"(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])(19|20)\d\d";





然后在你的代码中你应该使用:



Then in your code you should use:

foreach (Match match in Regex.Matches(yourString, expresion ))
{
string dateString = match.Value; //your date string: "20032014";
string dd = dateString.Substring(0,2);
string mm= dateString.Substring(2,2);
string yyyy= dateString.Substring(4,4);
//use your data
//..
 
}





请注意,上面的代码通常可以匹配输入字符串中的多个日期。如果您只想管理第一次出现,可以使用,如果代替 foreach


根据修复数据的方式,有多种方法可以执行此操作。我只是猜测它不是特别的,我们可以信任的唯一部分是日期。

所以试试这个 - 它是一个正则表达式的组合来查找日期值,和转换为DateTime:

There are various ways to do this, depending on how "fixed" your data is. I'm just guessing that it isn't particularly, and the only part we can trust is the date.
So try this - it's a combination of a regular expression to find the date value, and a convert to DateTime:
string input = "Welcome20032014totheworld2014";
Regex getDate = new Regex(@"\d{8}");
Match m = getDate.Match(input);
if (m.Success)
    {
    DateTime date;
    if (DateTime.TryParseExact(m.Value, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
        {
        Console.WriteLine(date);
        }
    }


Chk this

Chk this
string a = "Welcome20032014totheworld2014";
           string b = string.Empty;
           int val;

           for (int i = 0; i < a.Length; i++)
           {
               if (Char.IsDigit(a[i]))
                   b += a[i];
           }
           textBox1.Text ="dd:"+ b.Substring(0, 2);
           textBox2.Text = "MM:" + b.Substring(2, 2);
           textBox3.Text = "YYYY:" + b.Substring(4, 4);


这篇关于找到给定字符串C *中的ddmmyyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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