Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 example:

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 character backwards.

How can I solve this using substrings or another method?

like image 890
user1958628 Avatar asked Dec 06 '22 10:12

user1958628


2 Answers

Use Substring in this case.

Retrieves a substring from this instance. The substring starts at a specified character position.

Try like this;

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

Here is a DEMO.

like image 108
Soner Gönül Avatar answered Dec 21 '22 04:12

Soner Gönül


You do not need to find index of .pdf

path.Substring(path.Length - 14, 10)
like image 32
Mehmet Ataş Avatar answered Dec 21 '22 02:12

Mehmet Ataş