Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to identify Date in a String

How to identify Date in a String

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    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

    S S R 3 Replies Last reply
    0
    • K K V Sekhar

      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

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K K V Sekhar

        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

        S Offline
        S Offline
        sph3rex
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K K V Sekhar

          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

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups