开发者

String manipulation question

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 ab开发者_开发百科out it? Please help.


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.


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);


you can use the Path class to fetch the file information.

 Path.GetExtension("youpath")


var extension = Path.GetExtension(Server.MapPath(@"D://MyDocuments/Pictures/Pic1.jpg"));


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


Simple use

string path = "D://MyDocuments/Pictures/Pic1.jpg";
            string extension = System.IO.Path.GetExtension(path);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜