Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Comments
[no name] 20-Mar-14 2:57am    
Where is the problem?
Sunny_Kumar_ 20-Mar-14 3:03am    
have you tried Regular Expression?
Raul Iloc 20-Mar-14 9:32am    
If my solution (solution 1) helped you, you should accept it!

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

The regular expression should be:

C#
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:
C#
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
//..
 
}


Note that the code above is generally and could match more then one date in your input string. If you want to manage only the first occurrence you could use if in place of foreach!
 
Share this answer
 
v3
Comments
Matt T Heffron 20-Mar-14 13:59pm    
Why use .Substring() when the Match has the individual pieces?
Just change your regex a bit:
const string expresion = @"(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((?:19|20)\d\d)";
and then the pieces are:
string dd = match.Groups[1].Value;
string mm = match.Groups[2].Value;
string yyyy = match.Groups[3].Value;
Raul Iloc 21-Mar-14 1:52am    
Yes, you right, but I wanted to offer the possibility to get the entire date string (and its validation), so the date string could be converted into DateTime object.
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:
C#
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);
        }
    }
 
Share this answer
 
Chk this
C#
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);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900