user175084 user175084 - 3 months ago 19
C# Question

remove last word in label split by \

Ok i have a string where i want to remove the last word split by \

for example:

string name ="kak\kdk\dd\ddew\cxz\"


now i want to remove the last word so that i get a new value for name as

name= "kak\kdk\dd\ddew\"

is there an easy way to do this

thanks

Answer

How do you get this string in the first place? I assume you know that '\' is the escape character in C#. However, you should get far by using

name = name.TrimEnd('\\')
name = name.Remove(name.LastIndexOf('\\') + 1);