C # substring in the middle

I have the following data:

D:\toto\food\Cloture_49000ert1_10_01_2013.pdf D:\toto\food\Cloture_856589_12_01_2013.pdf D:\toto\food\Cloture_66rr5254_10_12_2012.pdf 

How can I extract the date part? For instance:

 D:\toto\food\Cloture_49000ert1_10_01_2013.pdf --> 10_01_2013 D:\toto\food\Cloture_856589_12_01_2013.pdf --> 12_01_2013 D:\toto\food\Cloture_66rr5254_10_12_2012.pdf --> 10_12_2012 

My idea is to use LastIndexOf(".pdf") and then count 10 characters back.

How can I solve this using substrings or another method?

+4
source share
7 answers

Use Substring in this case.

Gets a substring from this instance. The substring begins with the specified character position.

Try it:

 string s = "D:\\toto\\food\\Cloture_490001_10_01_2013.pdf"; string newstring = s.Substring(s.Length - 14, 10); Console.WriteLine(newstring); 

Here is the DEMO .

+4
source

You do not need to search for the .pdf index

 path.Substring(path.Length - 14, 10) 
+4
source

I would do it with Regex.

 ^[\w:\\]+cloture_(\d+)_([\d_]+).pdf$ 

Will match the date in the second group.

+3
source

If the file name is always in this format, you can do something crude like this:

 string filename = @"D:\toto\food\Cloture_490001_10_01_2013.pdf"; string date = filename.Substring(filename.Length - 14, 10); 

This will get a substring of 10_01_2013.pdf with a length of 14 characters, but will only take the first 10 characters, leaving you with 10_01_2013 .

If, however, the file name is in a different format, and the date may appear anywhere inside the name, you can consider something like Regular Expressions to be able to match for ##_##_#### and pull it out.

+2
source

try this approach:

 string dateString = textString.Substring(textString.Length-14, 10); 

see here: Extract only the right most n letters from the string

0
source

If you want to use LastIndexOf, then

 string str = @"D:\toto\food\Cloture_490001_10_01_2013.pdf"; string temp = str.Substring(str.LastIndexOf(".pdf") - 10, 10); 

And you can make it out like

 DateTime dt; if(DateTime.TryParseExact(temp, "MM_dd_yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) { //valid } else { //invalid } 
0
source

I would think of using LastIndexOf ".pdf" and then a countdown. Or use the Path.GetFileNameWithoutExtension method to just get the name and then take the last 10 characters.

These methods will work if the path to the file names changes (which is likely to happen) and does not rely on magic numbers (different from the one that determines the length of the substring of interest to us) to find the right place in the line.

0
source

All Articles