Question

I reading date string of format February 01, 2012. I am looping from First Char and counting 16 from first to get date string and send to Parse extract to get datetime value.

Since Month name will be different then like April length is not equal to December length so I am not getting exact value to send to parseextract.

for (int i = 1; i <= 16; i++)
      {
      // read each character
       }

If I take April 01, 2012 and run above code then I will get extra char after 16th character.

Is there any way to implement above logic?

Was it helpful?

Solution 2

You could compare next character with a , and you know that it's 5 chars after ,:

var i = 0;
while (true)
{
    // read each character
    var c = nextChar[i];

    if(c == ',')
    {
        // read exactly five more chars

        break; // exit infinite loop
    }

    i++;
}

OTHER TIPS

You could use a regular expression to do this:

void Main()
{
    var regex = new Regex(@"(January|February|March|April|May|June|July|August|September|October|November|December) \d{2}, \d{4}");
    string test1 = "Lorem Ipsum February 01, 2012";
    string test2 = "Lorem Ipsum Badmonth 01, 2012";
    regex.Match(test1).Dump();
    regex.Match(test2).Dump();
}

if you want the search to be case insensitive (will work with February or february) replace it with:

var regex = new Regex(@"(January|February|March|April|May|June|July|August|September|October|November|December) \d{2}, \d{4}", RegexOptions.IgnoreCase);

or (they exactly do the same thing)

var regex = new Regex(@"(?i)(January|February|March|April|May|June|July|August|September|October|November|December) \d{2}, \d{4}");

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top