更改"/"分隔字符串中最后一个元素的最有效方法是什么

Lan*_*opp 2 c#

我在c#中有一个字符串对象,其中包含一些由'/'字符分隔的元素.字符串看起来像这样:

"部件1/element2的/元素3 /元素4"

更改'/'分隔字符串中最后一个元素的最有效方法是什么?

Mar*_*ers 11

用途string.LastIndexOf:

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


Joe*_*orn 5

如果这是文件名/路径字符串,则应使用System.IO.Pathfor.