How to convert
"String path = @"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
into
String path = @"C:\Abc\Omg\Why\Me\"
.
My approach is to first reverse
the string
and then remove all the "\"
till we get first char
, and the reverse
it again.
How to do this in C#, is there any method for such operation?
Double Backslashes (\\)Two backslashes are used as a prefix to a server name (hostname). For example, \\a5\c\expenses is the path to the EXPENSES folder on the C: drive on server A5. See UNC, \\, path and forward slash.
TrimEnd(new[] { '/' }); With this method you can also specify multiple characters so you may want to remove all slash or backslash using the array. 1string fileName = "Test\\\\"; fileName= fileName. TrimEnd(new[] { '/', '\\\\' });
Replace(@"\\",@""); String abc = result. Replace(@"\\",string. Empty);
Using strip() Function to Remove Backslash from String in Python.
You can just construct path using the Path
static class:
string path = Path.GetFullPath(@"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\");
After this operation, variable path
will contain the minimal version:
C:\Abc\Omg\Why\Me\
You can use path.TrimEnd('\\')
. Have a look at the documentation for String.TrimEnd
.
If you want the trailing slash, you can add it back easily.
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