2

In a C# application, I get the Desktop folder doing this :

string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

Which gives this string : "C:\\Users\\username\\Desktop". As you see there are two slashes, which is problematic. Is there an easy way to delete a slash each time I meet them ?

Thank you in advance.

ALGR
  • 319
  • 1
  • 10
  • 2
    If you're viewing that in the debugger, those aren't repetitive characters, they're ***escape*** characters. To represent a literal backslash in a string literal, it must be preceded by a backslash (or 'escaped'). The actual value of that string is `C:\Users\username\Desktop`. – p.s.w.g Apr 22 '16 at 00:37

1 Answers1

4

Just so you know, the "\\" is actually one character - a backslash is an escape character (it is used in things like \r or \n). Since it is an escape character, to get a string representation of an actual backslash, you have to escape it, leading to the double backslash, "\\".

TLDR: "\\" in a string actually represents a single backslash.

If you want to verify this, try printing out the string "\\".

In general, to remove a duplicate character, you can use the .Replace function:

mystring.Replace("xx", "x");
nhouser9
  • 6,730
  • 3
  • 21
  • 42