remove last word in label split by \

This regex replacement should do the trick:

name = Regex.Replace(name, @"\\[a-z]*\\$", "\\");

string result = string.Join("\\",
            "kak\\kdk\\dd\\ddew\\cxz\\"
            .Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)
            .Reverse()
            .Skip(1)
            .Reverse()
            .ToArray()) + "\\";

Here's a non-regex manner of doing it.

string newstring = name.SubString(0, name.SubString(0, name.length - 1).LastIndexOf('\\'));

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('\\').Remove(name.LastIndexOf('\\') + 1);

Tags:

C#