Solved

how to parse exact day"th"

Posted on 2011-02-21
5
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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