English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm dealing with processing of data.

Currently, I'm stucked with this dilemma. I have different set of strings as shown below.

Example:
String 1 = " at March 12, 2004 16:34 by Mark Jacobs";
String 2 = " at March 12, 2004 16:34";
String 3 = " March 12, 2004 16:34 by Mark Jacobs";
String 4 = "March 12, 2004 16:34 ";

I need just to extract the DateTime variable from the individual string. There is no problem of conversion for String 4 since it is quite a straightforward conversion. But my problem will be String 1, String 2 and String 3.

I'm coding it using C#.
Been pondering on how to deal with this for some time.

Hope someone can give me ideas or codes to deal with this.

Thanks!

2007-03-02 05:30:22 · 4 answers · asked by dz7en 1 in Computers & Internet Programming & Design

4 answers

The way *I* would do it, is as follows:

you need to know the beginning of the string. If it's always in the above format (i.e., it always starts with the name of the month), just try to look up each of the months in the string until you find one. Cut off everything before the position where the name of the month is found.

Add a space to the end of the resulting string, and split this string based on the space character (string[] myField = myString.split(' ')).

myField[0] now contains the day (with a trailing comma; remove!),
myField[1] contains the year, and
myField[2] contains the time.

All other fields can be ignored. Using these three fields, if necessary, you can re-create a parseable date using

string myDate = String.Format("{0}, {1} {2}", myField[0], myField[1], myField[2]).

Hope that helps!

2007-03-02 05:52:12 · answer #1 · answered by Anonymous · 0 0

If those are your only string configurations you can check for 2 situations. I'm a VB programmer so this is psuedo and you'll have to convert to C#.

IF first two letters of string are "at" the trim off the first 3 characters.

For the second part, read the last character of the string.
If it's not a number, trim it off. LOOP until you hit a number, which should be the last digit of your time.

hope that helps

2007-03-02 05:49:05 · answer #2 · answered by rod 6 · 0 0

Your app shouldn't be doing that in the first place, saving date & time with other strings in the same field. Instead of handling it in your app, you should restructure the table and move any non-datetime values that you may need to another field, and recode the app not to mix them up in the future.

Otherwise you're stuck with having to filter/clean those strings each time you access them in your code. You can use string functions like IndexOf, Substring, and Replace to do that.

2007-03-02 06:15:52 · answer #3 · answered by Raymond 3 · 0 0

You could split your entire string into a string array. Then, loop through your array and use DateTime.TryParse() each element of the array.

If the element DOESN'T pass the parse attempt, throw it out. Then, construct a DateTime variable from the remaining elements.

2007-03-02 08:30:17 · answer #4 · answered by Kryzchek 4 · 0 0

fedest.com, questions and answers