How to identify Date in a String
-
Hi all, How to identify Date in a String ? The string may contains different sysmbols and alpha numeric values including Date (like hj/dfsks jh/f12/353 @@2df 23/11/2008 kjdl jf\ioi jr4398 rjeo i") Please suggest me how to find that. Thanks in advance
-
Hi all, How to identify Date in a String ? The string may contains different sysmbols and alpha numeric values including Date (like hj/dfsks jh/f12/353 @@2df 23/11/2008 kjdl jf\ioi jr4398 rjeo i") Please suggest me how to find that. Thanks in advance
You can use regular expressions to parse strings and extract data formatted in specific ways. You will need to create a Regex[^] object with the require pattern you want to match. There is a guide to .net regular expressions here[^]. This is also quite a good resource for regular expressions: http://www.regular-expressions.info/[^] You might want to consider downloading something like Expresso[^] to test your regular expression patterns.
Simon
-
Hi all, How to identify Date in a String ? The string may contains different sysmbols and alpha numeric values including Date (like hj/dfsks jh/f12/353 @@2df 23/11/2008 kjdl jf\ioi jr4398 rjeo i") Please suggest me how to find that. Thanks in advance
Rather than using the regex aproach i would suggest Date.TryParse(in "string containing date", out date_object) ... as date varies on localization it might be a lil overhead building a regex to handle all kinds of formatting and might require, depending on case, a lot more processing time.
Code? Yeah i love it fried together with a glass of wine.
-
Hi all, How to identify Date in a String ? The string may contains different sysmbols and alpha numeric values including Date (like hj/dfsks jh/f12/353 @@2df 23/11/2008 kjdl jf\ioi jr4398 rjeo i") Please suggest me how to find that. Thanks in advance
Based on the string you provided...
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
string myString = "hj/dfsks jh/f12/353 @@2df 23/11/2008 kjdl jf\\ioi jr4398 rjeo i";
string[] parts = myString.Split(' ');
DateTime dateFound;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;foreach (string part in parts) { DateTime date; if (DateTime.TryParseExact(part, "dd/MM/yyyy", culture, styles, out date)) { dateFound = date; break; } } } }
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001