2011-07-12 105 views
0

如果我有这个字符串:字符串处理问题

D://MyDocuments/Pictures/Pic1.jpg 

,我想提取“.JPG”出这个字符串就是我想要的(点)(扩展)

我该如何去关于它?请帮忙。

回答

3

它可以使用子,但它的更好,如果你有Path.GetExtension做它做

string fileName = @"C:\mydir.old\myfile.ext"; 
string path = @"C:\mydir.old\"; 
string extension; 

extension = Path.GetExtension(fileName); 
5

看一看使用Path.GetExtension Method

指定路径的延伸(包括句号 “”),或 null,或者的String.Empty。如果path为null,则GetExtension返回null。如果 路径没有扩展信息,则GetExtension返回 String.Empty。

3

您可以使用Path类来获取文件信息。

Path.GetExtension("youpath") 
2

对于文件名,看看System.IO.Pathstatic members。你会发现很多方法。

如果你想坚持的字符串操作,这样的事情将是很好:

string wholeName = @"D:\MyDocuments\Pictures\Pic1.jpg"; 
int dotPosition = wholeName.LastIndexOf('.'); // find last dot 
string ext = wholeName.Substring(dotPosition); // get out the extenstion 
3
var extension = Path.GetExtension(Server.MapPath(@"D://MyDocuments/Pictures/Pic1.jpg")); 
1

简单使用

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