DirectoryPath
= C:\照片管理C#篩選差異
filePath
= C:\照片管理\狗\ dog.PNG
newPath
應該是:狗\ dog.PNG
如何獲得newPath
? 我的代碼片段是無法事先正確
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問題的情況下, ](https://meta.stackexchange.com/a/66378/208223)。你確定你想要的字符串的差異,而不是相對路徑? – Theraot
'LastIndexOf'和'IndexOf'返回子字符串開始的索引,而不是結束的地方。 – juharr