user1958628
user1958628

Reputation: 409

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?

Upvotes: 4

Views: 28464

Answers (7)

Soner Gönül
Soner Gönül

Reputation: 98740

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.

Upvotes: 6

ChrisF
ChrisF

Reputation: 137128

I'd go with your idea of using LastIndexOf ".pdf" and then count backwards. Or use the Path.GetFileNameWithoutExtension method to just get the name and then take the last 10 characters.

These methods will both keep working if the path to the filenames ever changes (which it probably will) and don't rely on magic numbers (other than the one that defines the length of the substring we are interested in) to find the right place in the string.

Upvotes: 0

Bilal Hashmi
Bilal Hashmi

Reputation: 1485

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 parse it like

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

Upvotes: 0

Echilon
Echilon

Reputation: 10244

I'd do this with a Regex.

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

Would match the date in the second group.

Upvotes: 3

Rudi Visser
Rudi Visser

Reputation: 21969

If the filename is always in that format, you could do something crude like this:

string filename = @"D:\toto\food\Cloture_490001_10_01_2013.pdf";

string date = filename.Substring(filename.Length - 14, 10);

That will get a substring from 10_01_2013.pdf, which is 14 characters long, but only take the first 10 characters, leaving you with 10_01_2013.

If, however, the filename is in a different format and the date could appear anywhere within the name, you may want to consider something like Regular Expressions to be able to do a match for ##_##_#### and pull that out.

Upvotes: 2

Mehmet Ataş
Mehmet Ataş

Reputation: 11549

You do not need to find index of .pdf

path.Substring(path.Length - 14, 10)

Upvotes: 4

Davide Piras
Davide Piras

Reputation: 44605

try this approach:

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

see here as well: Extract only right most n letters from a string

Upvotes: 0

Related Questions