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. replace

replace

Scheduled Pinned Locked Moved C#
testingbeta-testingquestion
12 Posts 7 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.
  • F Offline
    F Offline
    fmardani
    wrote on last edited by
    #1

    Hi, This is what I have which is assigned to a string variable. I would like to replace the \\ to \ How is this done please? I tried: string strData = strData.Replace("\\", @"\") But it does not work. Thanks strData = "{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0{\\fonttbl\r\n{\\f0\\fswiss\\fcharset0\\fprq2 Arial;}\r\n{\\f1\\froman\\fcharset2\\fprq2 Symbol;}}\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\r\n{\\stylesheet{\\s0\\itap0\\f0\\fs24 [Normal];}{\\*\\cs10\\additive Default Paragraph Font;}}\r\n{\\*\\generator TX_RTF32 12.0.500.501;}\r\n\\deftab1134\\paperw12240\\paperh15840\\margl1440\\margt1440\\margr1440\\margb1440\\pard\\itap0\\plain\\f0\\fs24 is is\\plain\\f0\\fs20 to \\plain\\f0\\fs20\\i confirm\\plain\\f0\\fs20\\par for \\plain\\f0\\fs20\\ul testing\\plain\\f0\\fs20\\b reasons...\\par }"

    R J G 3 Replies Last reply
    0
    • F fmardani

      Hi, This is what I have which is assigned to a string variable. I would like to replace the \\ to \ How is this done please? I tried: string strData = strData.Replace("\\", @"\") But it does not work. Thanks strData = "{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0{\\fonttbl\r\n{\\f0\\fswiss\\fcharset0\\fprq2 Arial;}\r\n{\\f1\\froman\\fcharset2\\fprq2 Symbol;}}\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\r\n{\\stylesheet{\\s0\\itap0\\f0\\fs24 [Normal];}{\\*\\cs10\\additive Default Paragraph Font;}}\r\n{\\*\\generator TX_RTF32 12.0.500.501;}\r\n\\deftab1134\\paperw12240\\paperh15840\\margl1440\\margt1440\\margr1440\\margb1440\\pard\\itap0\\plain\\f0\\fs24 is is\\plain\\f0\\fs20 to \\plain\\f0\\fs20\\i confirm\\plain\\f0\\fs20\\par for \\plain\\f0\\fs20\\ul testing\\plain\\f0\\fs20\\b reasons...\\par }"

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      string strData = strData.Replace(@"\\", @"\") or string strData = strData.Replace("\\\\", "\\") /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • J James Gupta

        C# reads "\\" as one slash, so type "\\\\" instead

        J Offline
        J Offline
        James Gupta
        wrote on last edited by
        #3

        sorry, did not see other post - tired :S

        1 Reply Last reply
        0
        • F fmardani

          Hi, This is what I have which is assigned to a string variable. I would like to replace the \\ to \ How is this done please? I tried: string strData = strData.Replace("\\", @"\") But it does not work. Thanks strData = "{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0{\\fonttbl\r\n{\\f0\\fswiss\\fcharset0\\fprq2 Arial;}\r\n{\\f1\\froman\\fcharset2\\fprq2 Symbol;}}\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\r\n{\\stylesheet{\\s0\\itap0\\f0\\fs24 [Normal];}{\\*\\cs10\\additive Default Paragraph Font;}}\r\n{\\*\\generator TX_RTF32 12.0.500.501;}\r\n\\deftab1134\\paperw12240\\paperh15840\\margl1440\\margt1440\\margr1440\\margb1440\\pard\\itap0\\plain\\f0\\fs24 is is\\plain\\f0\\fs20 to \\plain\\f0\\fs20\\i confirm\\plain\\f0\\fs20\\par for \\plain\\f0\\fs20\\ul testing\\plain\\f0\\fs20\\b reasons...\\par }"

          J Offline
          J Offline
          James Gupta
          wrote on last edited by
          #4

          C# reads "\\" as one slash, so type "\\\\" instead

          J 1 Reply Last reply
          0
          • F fmardani

            Hi, This is what I have which is assigned to a string variable. I would like to replace the \\ to \ How is this done please? I tried: string strData = strData.Replace("\\", @"\") But it does not work. Thanks strData = "{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0{\\fonttbl\r\n{\\f0\\fswiss\\fcharset0\\fprq2 Arial;}\r\n{\\f1\\froman\\fcharset2\\fprq2 Symbol;}}\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\r\n{\\stylesheet{\\s0\\itap0\\f0\\fs24 [Normal];}{\\*\\cs10\\additive Default Paragraph Font;}}\r\n{\\*\\generator TX_RTF32 12.0.500.501;}\r\n\\deftab1134\\paperw12240\\paperh15840\\margl1440\\margt1440\\margr1440\\margb1440\\pard\\itap0\\plain\\f0\\fs24 is is\\plain\\f0\\fs20 to \\plain\\f0\\fs20\\i confirm\\plain\\f0\\fs20\\par for \\plain\\f0\\fs20\\ul testing\\plain\\f0\\fs20\\b reasons...\\par }"

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

            I think that you should look at the real problem, not just the symptom. Why do you have a string that contains double backslashes? Where does the string come from? I see that you also have escape codes like \r and \n in the string. If you don't handle them first, you will end up with a string that contains some "\r" that means Carrege Return, and some that doesn't. --- b { font-weight: normal; }

            L 1 Reply Last reply
            0
            • G Guffa

              I think that you should look at the real problem, not just the symptom. Why do you have a string that contains double backslashes? Where does the string come from? I see that you also have escape codes like \r and \n in the string. If you don't handle them first, you will end up with a string that contains some "\r" that means Carrege Return, and some that doesn't. --- b { font-weight: normal; }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Guffa wrote:

              Why do you have a string that contains double backslashes? Where does the string come from?

              Looks like an RTF file header.

              G 1 Reply Last reply
              0
              • L Lost User

                Guffa wrote:

                Why do you have a string that contains double backslashes? Where does the string come from?

                Looks like an RTF file header.

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

                Yes, I recognise the rtf code, but that is not the point. Where does the string come from, as it contains escaped characters? If it would have been read from an rtf file, it woudln't. --- b { font-weight: normal; }

                M 1 Reply Last reply
                0
                • G Guffa

                  Yes, I recognise the rtf code, but that is not the point. Where does the string come from, as it contains escaped characters? If it would have been read from an rtf file, it woudln't. --- b { font-weight: normal; }

                  M Offline
                  M Offline
                  mav northwind
                  wrote on last edited by
                  #8

                  Why does this matter? You can have a hardcoded rtf string somewhere to preset the contents of a RichTextBox for example, and the sample the original author gave here looks just like such a case. Besides it seems to be perfectly good rtf, he just misinterpreted the escaped backslashes... Regards, mav

                  J G 2 Replies Last reply
                  0
                  • M mav northwind

                    Why does this matter? You can have a hardcoded rtf string somewhere to preset the contents of a RichTextBox for example, and the sample the original author gave here looks just like such a case. Besides it seems to be perfectly good rtf, he just misinterpreted the escaped backslashes... Regards, mav

                    J Offline
                    J Offline
                    jasonpb
                    wrote on last edited by
                    #9

                    I agree. What does it matter where the string came from? He simply wanted to know how to fix the string by replacing the double backslashes with singles. Problem was solved. -Jason

                    G 1 Reply Last reply
                    0
                    • M mav northwind

                      Why does this matter? You can have a hardcoded rtf string somewhere to preset the contents of a RichTextBox for example, and the sample the original author gave here looks just like such a case. Besides it seems to be perfectly good rtf, he just misinterpreted the escaped backslashes... Regards, mav

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

                      mav.northwind wrote:

                      Why does this matter? You can have a hardcoded rtf string somewhere to preset the contents of a RichTextBox for example, and the sample the original author gave here looks just like such a case.

                      If it were a hardcoded string, the obvious solution would of course be to correct the string in the code instead of correcting it afterwards.

                      mav.northwind wrote:

                      Besides it seems to be perfectly good rtf, he just misinterpreted the escaped backslashes...

                      If it really was actual code that the original author showed, the problem does not exist at all, so the correct solution would be to do nothing at all. What the best solution is, is still unknown until we know where the string comes from. That why I asked. --- b { font-weight: normal; } -- modified at 3:58 Thursday 2nd February, 2006

                      M 1 Reply Last reply
                      0
                      • J jasonpb

                        I agree. What does it matter where the string came from? He simply wanted to know how to fix the string by replacing the double backslashes with singles. Problem was solved. -Jason

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

                        jasonpb wrote:

                        What does it matter where the string came from? He simply wanted to know how to fix the string by replacing the double backslashes with singles. Problem was solved.

                        As the string contains both escaped control characters and escaped backslashes, simply replacing the backslashes will not solve the whole problem. It's not very uncommon that the real problem is different from what the original poster thinks. If you then only answer the question, you haven't solved the problem. Many times I have not only provided the correct answer, but also the correct question. Just because people don't know what to ask for, should we deprave them of a good answer? --- b { font-weight: normal; }

                        1 Reply Last reply
                        0
                        • G Guffa

                          mav.northwind wrote:

                          Why does this matter? You can have a hardcoded rtf string somewhere to preset the contents of a RichTextBox for example, and the sample the original author gave here looks just like such a case.

                          If it were a hardcoded string, the obvious solution would of course be to correct the string in the code instead of correcting it afterwards.

                          mav.northwind wrote:

                          Besides it seems to be perfectly good rtf, he just misinterpreted the escaped backslashes...

                          If it really was actual code that the original author showed, the problem does not exist at all, so the correct solution would be to do nothing at all. What the best solution is, is still unknown until we know where the string comes from. That why I asked. --- b { font-weight: normal; } -- modified at 3:58 Thursday 2nd February, 2006

                          M Offline
                          M Offline
                          mav northwind
                          wrote on last edited by
                          #12

                          I think the simple explanation is that fmardani pasted something into his RTB and looked at the Rtf property in the VS debugger. I guess that's what a lot of people do in the beginning to get into rtf. That's exactly the format you get there (including the missing semicolon after his strData = ... assignment) :) But you're right, the RTF string itself is not the real source for the author's problems but a general misunderstanding regarding escaped characters and the '@' notation. Regards, mav

                          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