【发布时间】:2017-04-11 11:25:24
【问题描述】:
DirectoryPath = C:\Pics
filePath = C:\Pics\Dogs\dog.PNG
newPath 应该是:Dogs\dog.PNG
如何获得newPath?
我的代码 sn-p 不对
string directoryPath = "C:\\Pics";
string filePath = "C:\\Pics\\Dogs\\dog.PNG";
if (!directoryPath.EndsWith("\\"))
directoryPath = directoryPath + "\\";
string newPath = filePath.Substring(filePath.LastIndexOf(directoryPath) + 1);
提前致谢!
【问题讨论】:
-
看起来像XY problem 的情况。您确定要区分字符串而不是相对路径吗?
-
LastIndexOf和IndexOf返回子字符串开始的索引,而不是结束的索引。