Sim*_*ead 47
string path = "C://hello//world";
int pos = path.LastIndexOf("/") + 1;
Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "world"
Run Code Online (Sandbox Code Playgroud)
该LastIndexOf方法的执行方式与...相同,IndexOf但是从字符串的末尾开始.
Mat*_*kan 18
using System.Linq;
var s = "C://hello//world";
var last = s.Split('/').Last();
Run Code Online (Sandbox Code Playgroud)
Dus*_*gen 14
有一个用于处理Paths的静态类Path.
您可以使用完整的文件名Path.GetFileName.
要么
您可以获取没有扩展名的文件名Path.GetFileNameWithoutExtension.
小智 7
试试这个:
string worldWithPath = "C://hello//world";
string world = worldWithPath.Substring(worldWithPath.LastIndexOf("/") + 1);
Run Code Online (Sandbox Code Playgroud)
我建议查看System.IO命名空间,因为您可能想要使用它。还有 DirectoryInfo 和 FileInfo 也可能在这里有用。特别是DirectoryInfo 的 Name 属性
var directoryName = new DirectoryInfo(path).Name;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41366 次 |
| 最近记录: |