Reputation: 1325
If I have this string:
D://MyDocuments/Pictures/Pic1.jpg
and I want to extract ".jpg" out of this string ie I want the (dot)(extension)
How do I go about it? Please help.
Upvotes: 0
Views: 199
Reputation: 19956
For filenames, look into System.IO.Path
static members. You'll find plenty of methods there.
If you want to stick with string manipulation, something like this would be nice:
string wholeName = @"D:\MyDocuments\Pictures\Pic1.jpg";
int dotPosition = wholeName.LastIndexOf('.'); // find last dot
string ext = wholeName.Substring(dotPosition); // get out the extenstion
Upvotes: 2
Reputation: 3997
Simple use
string path = "D://MyDocuments/Pictures/Pic1.jpg";
string extension = System.IO.Path.GetExtension(path);
Upvotes: 1
Reputation: 13308
var extension = Path.GetExtension(Server.MapPath(@"D://MyDocuments/Pictures/Pic1.jpg"));
Upvotes: 3
Reputation: 5604
Its can be done using substring but its better if you do it with Path.GetExtension
string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension;
extension = Path.GetExtension(fileName);
Upvotes: 3
Reputation: 4619
you can use the Path class to fetch the file information.
Path.GetExtension("youpath")
Upvotes: 3
Reputation: 166396
Have a look at using Path.GetExtension Method
The extension of the specified path (including the period "."), or null, or String.Empty. If path is null, GetExtension returns null. If path does not have extension information, GetExtension returns String.Empty.
Upvotes: 5