c# - How can I parse a DateTime from a string containing extra characters? -


i have string. string this

hello coder, dob 12/09/2011.

i want extract date sentence. how do using c#? not want regular expressions. interview question asked me recently.

this try

string mystr = "hello 12/3/2013";             datetime s;             datetime.tryparse(mystr,out s);             console.writeline(s); 

i getting output

01-01-0001 00:00:00 

both c# , javascript support regular expressions. can use pattern find section of string:

\d{2}/\d{2}/\d{4} 

of course doesn't ensure it's valid date, e.g. 13/88/0000 match pattern. you'd have parse string using date.parse.


however, since you've stated regular expressions not option, here's very crude one-liner:

var input = "hello coder, dob 12/09/2011."; datetime date = new datetime(); input.split().skipwhile(s => !datetime.tryparse(s, out date)).any();  console.writeline(date); // 12/9/2011 12:00:00 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -