2010-06-10 56 views

回答

11

使用string.LastIndexOf

string s = "element1/element2/element3/element4"; 
s = s.Substring(0, s.LastIndexOf('/') + 1) + "foo"; 
+0

嘿,这正是这个想法^^ – Ismael 2010-06-10 21:36:23

+0

嘿,这是一个非常聪明的解决方案。谢谢。 – LandonSchropp 2010-06-10 21:39:52

5

如果这是一个文件名/路径字符串,你应该使用System.IO.Path这一点。

1

C#String类中有'lastIndexOf'吗? (我通常不用C#编写代码),如果存在,可以使用它来获取对字符串中最后一个/的引用,并且该字符串的最后一个元素位于/之前。

0

像乔尔建议..也许是这样的:

 string path = (System.IO.Path.GetDirectoryName(@"element1/element2/element3/element4") + 
     System.IO.Path.DirectorySeparatorChar + "foo"); 
    string new_path = path.Replace(System.IO.Path.DirectorySeparatorChar, '/'); 
相关问题