Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting double slashes

Converting double slashes

Scheduled Pinned Locked Moved C#
databasequestion
21 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Martin 0

    Replace[^]

    filename.Replace("\\","\");

    -- modified at 9:06 Friday 27th July, 2007 trash post, sorry!

    All the best, Martin

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #10

    Martin# wrote:

    filename.Replace("\\","\");

    That won't even compile. What you intended to write was: filename.Replace("\\\\","\\"); or filename.Replace(@"\\",@"\"); However, that will not do anything at all, as the string doesn't contain any double backslashes.

    --- single minded; short sighted; long gone;

    M 1 Reply Last reply
    0
    • M Mark06

      appologies, I was trying to keep it simple. I'm actually calling the stored proc 'sp_attach_db' and I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #11

      That's not surprising, both literals end up as a single backslash. I suggest you post the actual problem you're encountering on the SQL/ADO/ADO.NET forum.

      Stability. What an interesting concept. -- Chris Maunder

      M 1 Reply Last reply
      0
      • G Guffa

        Martin# wrote:

        filename.Replace("\\","\");

        That won't even compile. What you intended to write was: filename.Replace("\\\\","\\"); or filename.Replace(@"\\",@"\"); However, that will not do anything at all, as the string doesn't contain any double backslashes.

        --- single minded; short sighted; long gone;

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #12

        :doh: Sure!

        All the best, Martin

        1 Reply Last reply
        0
        • M Martin 0

          Replace[^]

          filename.Replace("\\","\");

          -- modified at 9:06 Friday 27th July, 2007 trash post, sorry!

          All the best, Martin

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #13

          LOL, that wont compile :)

          1 Reply Last reply
          0
          • M Mike Dimmick

            That's not surprising, both literals end up as a single backslash. I suggest you post the actual problem you're encountering on the SQL/ADO/ADO.NET forum.

            Stability. What an interesting concept. -- Chris Maunder

            M Offline
            M Offline
            Mark06
            wrote on last edited by
            #14

            but this isnt an sql problem. the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'" the actual sql command is irrelevant. its the double slashes thats the issue.

            G M 2 Replies Last reply
            0
            • M Mark06

              appologies, I was trying to keep it simple. I'm actually calling the stored proc 'sp_attach_db' and I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #15

              Mark06 wrote:

              I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.

              Of course not. You are replacing each single backslash with a single backslash. Also, the Replace method doesn't change the string, it returns the new string, so you have to use the result of the method: str = str.Replace("\\\\", "\\"); Still, the string literal that you showed doesn't contain any double backslashes, so that will not have any effect at all. What has made you come to the conclusion that the database has any problems with double backslashes, and why do you think that your string contains any?

              --- single minded; short sighted; long gone;

              1 Reply Last reply
              0
              • M Mark06

                but this isnt an sql problem. the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'" the actual sql command is irrelevant. its the double slashes thats the issue.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #16

                Mark06 wrote:

                the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'"

                It doesn't. Why do you think that it would?

                --- single minded; short sighted; long gone;

                M 1 Reply Last reply
                0
                • M Mark06

                  but this isnt an sql problem. the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'" the actual sql command is irrelevant. its the double slashes thats the issue.

                  M Offline
                  M Offline
                  Mike Dimmick
                  wrote on last edited by
                  #17

                  Visual Studio shows escapes in the C# debugger watch window and tooltips. I think that's a stupid idea, but there you are: that's what it does. If you output the string to the console (Console.WriteLine) you will see that the backslashes are not doubled. If you're having trouble attaching the database, please ask on the other forum (including all error messages) - this is not the problem.

                  Stability. What an interesting concept. -- Chris Maunder

                  L 1 Reply Last reply
                  0
                  • G Guffa

                    Mark06 wrote:

                    the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'"

                    It doesn't. Why do you think that it would?

                    --- single minded; short sighted; long gone;

                    M Offline
                    M Offline
                    Mike Dimmick
                    wrote on last edited by
                    #18

                    Damn debugger watch window (and tooltips) shows it as doubled.

                    Stability. What an interesting concept. -- Chris Maunder

                    M 1 Reply Last reply
                    0
                    • M Mike Dimmick

                      Damn debugger watch window (and tooltips) shows it as doubled.

                      Stability. What an interesting concept. -- Chris Maunder

                      M Offline
                      M Offline
                      Mark06
                      wrote on last edited by
                      #19

                      Yup, that'll be it!! :mad:

                      1 Reply Last reply
                      0
                      • M Mark06

                        Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename: string filename = "c:\\temp\\LogFile.Txt"; my command is built: command.CommandText = "sp_UpdateLogFile '" + filename + "'"; however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes?

                        N Offline
                        N Offline
                        Nouman Bhatti
                        wrote on last edited by
                        #20

                        ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_String_Replace_1_d460c748.htm use filename.replace("\\","\")

                        1 Reply Last reply
                        0
                        • M Mike Dimmick

                          Visual Studio shows escapes in the C# debugger watch window and tooltips. I think that's a stupid idea, but there you are: that's what it does. If you output the string to the console (Console.WriteLine) you will see that the backslashes are not doubled. If you're having trouble attaching the database, please ask on the other forum (including all error messages) - this is not the problem.

                          Stability. What an interesting concept. -- Chris Maunder

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #21

                          Never, never apply Console.WriteLine on a suspicious variable. You might find the cause of a problem...

                          Luc Pattyn


                          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups