Extract dates from filename(C#3.0)

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-06-15T03:22:23Z Indexed on 2010/06/15 3:42 UTC
Read the original article Hit count: 150

Filed under:

I have a situation where I need to extract dates from the file names whose general pattern is [filename_]YYYYMMDD[.fileExtension]

e.g. "xxx_20100326.xls" or x2v_20100326.csv

The below program does the work

//Number of charecter in the substring is set to 8 
//since the length of YYYYMMDD is 8

public static string ExtractDatesFromFileNames(string fileName)
{

    return fileName.Substring(fileName.IndexOf("_") + 1, 8);
}

Is there any better option of achieving the same?

I am basically looking for standard practice.

I am using C#3.0 and dotnet framework 3.5

Thanks

© Stack Overflow or respective owner

Related posts about c#3.0