C# 带有多个分隔符的子字符串URL
我有一个URL,我需要它的子串。在下面的URL中,我只需要document.doc部分 URL是 localhost/abcd/abcs/document.doc 我试过了C# 带有多个分隔符的子字符串URL,c#,asp.net,C#,Asp.net,我有一个URL,我需要它的子串。在下面的URL中,我只需要document.doc部分 URL是 localhost/abcd/abcs/document.doc 我试过了 FilePath = r["FilePath"].ToString().Substring(0,1); 使用 返回指定路径字符串的文件名和扩展名 System.IO.Path.GetFileName(“localhost/abcd/abcs/document.doc”)//document.doc尝试以下操作: var p
FilePath = r["FilePath"].ToString().Substring(0,1);
使用
返回指定路径字符串的文件名和扩展名
System.IO.Path.GetFileName(“localhost/abcd/abcs/document.doc”)//document.doc
尝试以下操作:
var path = "localhost/abcd/abcs/document.doc";
var fileName = path.SubString(path.LastIndexOf("/")+1);
如果您确定r[“FilePath”]将始终返回字符串值且不为null,那么您可以在字符串变量中分配该值。然后使用Substring函数获取值。例如
string FilePath = r["FilePath"].ToString();
string filename = FilePath.SubString(FilePath.LastIndexOf("/")+1);
另一种选择如下
string FilePath = r["FilePath"].ToString();
string filename = FilePath.Split('/').ToList().Last();
我试过这样做,但ti说当前上下文中不存在文件名,这是子字符串部分FileName=r[“FilePath”].ToString().substring(FileName.LastIndexOf(“/”)+1)中的文件名,我试过这样做,但ti说当前上下文中不存在文件名,这是子字符串部分FileName=r[“FilePath”]中的文件名。ToString().Substring(FileName.LastIndexOf(“/”)+1),什么是
r[“FilePath”]
?确切的c#
类型是什么?
string FilePath = r["FilePath"].ToString();
string filename = FilePath.Split('/').ToList().Last();
FileInfo fi = new FileInfo(Server.MapPath(r["FilePath"].ToString()));
var filename = fi.Name;
var extension = fi.Extension;