"blahblah\dummytext\_config.bat"
Run Code Online (Sandbox Code Playgroud)
如果我想从这个字符串中获取_config.bat,那么最好的方法是什么?只有我能想到的方法是从.bat读回它找到的第一个斜线,但我不知道该怎么做.
你可以使用LastIndexOf方法找到最后一个反斜杠:
int index = myString.LastIndexOf( @"\" );
string result = myString.SubString( index + 1 );
Run Code Online (Sandbox Code Playgroud)
丹尼尔有一个很好的观点.使用FileInfo将是一种更安全的方式,并且易于使用:
// myString doesn't need a full path either
System.IO.FileInfo fi = new System.IO.FileInfo( myString );
// returns just the file name without the path
string result = fi.Name;
Run Code Online (Sandbox Code Playgroud)
agent-j的暗示:
string result = System.IO.Path.GetFileName( myString );
Run Code Online (Sandbox Code Playgroud)
我不确定Path是否想要一条完整的路径.FileInfo和Path都接受不存在的路径和文件名.
| 归档时间: |
|
| 查看次数: |
3307 次 |
| 最近记录: |