I need to extract the path info using Path.GetFileName()
, and this function doesn't work when the last character of the input string is DirectorySeparatorChar('/' or '\').
I came up with this code, but it's too lengthy. Is there a better way to go?
string lastCharString = fullPath.Substring (fullPath.Length-1); char lastChar = lastCharString[0]; if (lastChar == Path.DirectorySeparatorChar) { fullPath = fullPath.Substring(0, fullPath.Length-1); }
Every string in C ends with '\0'. So you need do this: int size = strlen(my_str); //Total size of string my_str[size-1] = '\0'; This way, you remove the last char.
slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.
fullPath = fullPath.TrimEnd(Path.DirectorySeparatorChar);
// If the fullPath is not a root directory if (Path.GetDirectoryName(fullPath) != null) fullPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With