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. string replace() problem

string replace() problem

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
6 Posts 6 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.
  • S Offline
    S Offline
    sysmatrix
    wrote on last edited by
    #1

    string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

    A C M S N 5 Replies Last reply
    0
    • S sysmatrix

      string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

      A Offline
      A Offline
      Alex Korchemniy
      wrote on last edited by
      #2

      You have to escape your chars :rolleyes: Alex Korchemniy

      1 Reply Last reply
      0
      • S sysmatrix

        string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        I suspect the problem is that \d and \t are valid escape values, so you've not realised the problem. To put a \ in your string, either put two or, ( my preferred option ), use @. With @ at the start, everything is literal, the only escape is double quotes to do quotes. string aa = "\document\test"; The value here is ocument test, where the spaces are a tab string aa = @"\document\test"; string aa = "\\document\\test"; These both set the string as you'd expect Christian Graus - Microsoft MVP - C++

        1 Reply Last reply
        0
        • S sysmatrix

          string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

          M Offline
          M Offline
          MoustafaS
          wrote on last edited by
          #4

          If you have it already and want it to be replaced now,you may use the StringBuilder class that will assure that it's been replaced.

          1 Reply Last reply
          0
          • S sysmatrix

            string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

            S Offline
            S Offline
            S Sansanwal
            wrote on last edited by
            #5

            string aa = @"\document\test"; aa = aa.Replace(@"\", @"\\") ; Sanjay Sansanwal www.sansanwal.com

            1 Reply Last reply
            0
            • S sysmatrix

              string aa = "\document\test"; aa = aa.Replace("\", "\\"); --- this line C# doesn't like it?? I want to replace '\' in string aa to "\\". How to do it?

              N Offline
              N Offline
              nemopeti
              wrote on last edited by
              #6

              string aa = @"\document\test"; //than use aa = aa.Replace("\\", "\\\\"); //or aa = aa.Replace(@"\", @"\\"); //both output is: \\document\\test Trick: If U want to use \ in strings U must duplicate it, or use @ before teh string. I hope it helps. Nemo

              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