Solved

how to parse exact day"th"

Posted on 2011-02-21
5
375 Views
Last Modified: 2012-08-14
How can I parse exact this string in c#?

It seems it works with DateTime.Parse() as long as the "th" is not there how can I fix this?

Friday 18th February 2011 8:11:04 AM
0
Comment
Question by:NewtonianB
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Ramone_Hamilton earned 300 total points
ID: 34946325
Just replace the th.

            string date = "Friday 18th February 2011 8:11:04 AM";
            date = date.Replace("th", string.Empty);
            date = date.Replace("rd", string.Empty);
            date = date.Replace("st", string.Empty);

            DateTime.Parse(date);

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
ID: 34947275
Not too bad idea from Ramone_Hamilton but be careful!

The code above may convert the line

Saturday 19th February 2011 8:11:04 AM

to

Satuay 19th February 2011 8:11:04 AM

Same problem with August - you will leave it with "Augu"...

So - the code needs to be a bit more complicated. You may use regular expressions to remove suffixes after digits or just look for digits and then check for suffixes (and remove them if exist).
0
 
LVL 6

Expert Comment

by:Ramone_Hamilton
ID: 34947367
That's a really good catch!  I myself detest regular expressions as they always beat me down.  I know this may be excessive, but what about taking your string, split it by the spaces and then do the replace?

Here is a revised version.

            string date = "Friday 18th February 2011 8:11:04 AM";

            string[] dates = date.Split(' ');

            dates[1] = dates[1].Replace("th", string.Empty);
            dates[1] = dates[1].Replace("rd", string.Empty);
            dates[1] = dates[1].Replace("st", string.Empty);

            StringBuilder newDate = new StringBuilder();

            newDate.Append(dates[0]).Append(" ").Append(dates[1]).Append(" ").Append(dates[2]).Append(" ").Append(
                dates[3]).Append(" ").Append(dates[4]).Append(" ").
                Append(dates[5]);

            DateTime.Parse(newDate.ToString()); 

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34947563
OK, I agree - again good idea, I'd go with splitting and replacing dates[1].

However to use StringBuilder to re-assemble the string is an overkill, I think.

I'd write it as :

string cleaneddateStr = dates[0] + " " + dates[1] + " " + dates[2] + " " + dates[3] + " " + dates[4] + " " + dates[5]

Compiler is clever enough to create just a one string here so - no much overhead. With StringBuilder - quite a lot of overhead. I tested this couple of years ago.

Anyway, it works
0
 

Author Comment

by:NewtonianB
ID: 35017585
we forgot "nd" also
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question