获取内容
答
string path = "C://hello//world";
int pos = path.LastIndexOf("/") + 1;
Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "world"
的LastIndexOf
方法执行相同IndexOf
..但是从字符串的结尾。
答
我会建议看看System.IO
命名空间,因为它似乎你可能想要使用它。还有DirectoryInfo和FileInfo也可以在这里使用。具体DirectoryInfo's Name property
var directoryName = new DirectoryInfo(path).Name;
答
答
using System.Linq;
var s = "C://hello//world";
var last = s.Split('/').Last();
答
试试这个:
string worldWithPath = "C://hello//world";
string world = worldWithPath.Substring(worldWithPath.LastIndexOf("/") + 1);
我曾经想过,但要注意的是,OP似乎不把重点放在一个文件,而是一个目录 – 2013-04-07 02:51:23