replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Etu

    replace

    Hi,

    I have a string:

    string c = "'abc' \"cde\", 'mno' \"xyz\",";

    how can I use the c.Replace(???, ???) method to have this string:

    "'abc' "cde", 'mno' "xyz","
    that is, all the backslashes are removed.

    Thanks,

    Etu

  • Bruce Wood

    #2
    Re: replace

    In the line of code you wrote:

    string c = "'abc' \"cde\", 'mno' \"xyz\",";

    there are no backslashes in the string. The backslashes you see are
    only a typographical convention that tells the compiler that the
    double-quote that follows each backslash is not the double-quote that
    ends the string, but that it should instead place a double-quote
    character in the string.

    The string stored internally after compilation looks like this:

    'abc' "cde", 'mno' "xyz",

    What are you trying to do in your program and what problems are you
    encountering, since backslashes in the string don't appear to be what's
    wrong?

    Comment

    • SerhaD Inanli

      #3
      Re: replace

      string c = "'abc' \"cde\", 'mno' \"xyz\",";
      string clearString = c.Replace('\\', ' '); // You must type backslash twice

      That's All

      Hopes This Helps

      "Etu" <etu_sho@yahoo. com> wrote in message
      news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > I have a string:
      >
      > string c = "'abc' \"cde\", 'mno' \"xyz\",";
      >
      > how can I use the c.Replace(???, ???) method to have this string:
      >
      > "'abc' "cde", 'mno' "xyz","
      > that is, all the backslashes are removed.
      >
      > Thanks,
      >
      > Etu
      >[/color]


      Comment

      • Etu

        #4
        Re: replace

        Thanks Bruce!

        The problem I have is actually my program first reads some strings
        (with \t, \" and so on) from an XML file and writes these strings to an
        Excel file. After the Excel file is saved, \t becomes a space and \"
        becomes ". Then when my program later reads the strings from the Excel
        file, it doesn't think the strings are the same as those in the XML
        file.

        That is why I was trying to modify the strings from the XML file by
        getting rid of the backslashes - try to do the same thing that Excel
        does, but could not find a way to do so.

        Thanks

        Bruce Wood wrote:[color=blue]
        > In the line of code you wrote:
        >
        > string c = "'abc' \"cde\", 'mno' \"xyz\",";
        >
        > there are no backslashes in the string. The backslashes you see are
        > only a typographical convention that tells the compiler that the
        > double-quote that follows each backslash is not the double-quote that
        > ends the string, but that it should instead place a double-quote
        > character in the string.
        >
        > The string stored internally after compilation looks like this:
        >
        > 'abc' "cde", 'mno' "xyz",
        >
        > What are you trying to do in your program and what problems are you
        > encountering, since backslashes in the string don't appear to be[/color]
        what's[color=blue]
        > wrong?[/color]

        Comment

        • Etu

          #5
          Re: replace

          Thanks SerhaD. However, I tried your code but could not get what I
          want.

          Thanks

          SerhaD Inanli wrote:[color=blue]
          > string c = "'abc' \"cde\", 'mno' \"xyz\",";
          > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
          twice[color=blue]
          >
          > That's All
          >
          > Hopes This Helps
          >
          > "Etu" <etu_sho@yahoo. com> wrote in message
          > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=green]
          > > Hi,
          > >
          > > I have a string:
          > >
          > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
          > >
          > > how can I use the c.Replace(???, ???) method to have this string:
          > >
          > > "'abc' "cde", 'mno' "xyz","
          > > that is, all the backslashes are removed.
          > >
          > > Thanks,
          > >
          > > Etu
          > >[/color][/color]

          Comment

          • Champika Nirosh

            #6
            Re: replace


            "SerhaD Inanli" <serhadinanli@h otmail.com> wrote in message
            news:eVo2nIMCFH A.3696@TK2MSFTN GP14.phx.gbl...[color=blue]
            > string c = "'abc' \"cde\", 'mno' \"xyz\",";
            > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
            twice

            Yes but will this make any difference... to the out put... c and clearString
            both are same as you out them...???
            [color=blue]
            >
            > That's All
            >
            > Hopes This Helps
            >
            > "Etu" <etu_sho@yahoo. com> wrote in message
            > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=green]
            > > Hi,
            > >
            > > I have a string:
            > >
            > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
            > >
            > > how can I use the c.Replace(???, ???) method to have this string:
            > >
            > > "'abc' "cde", 'mno' "xyz","
            > > that is, all the backslashes are removed.
            > >
            > > Thanks,
            > >
            > > Etu
            > >[/color]
            >
            >[/color]


            Comment

            • Etu

              #7
              Re: replace

              Thanks Bruce!

              The problem I have is actually my program first reads some strings
              (with \t, \" and so on) from an XML file and writes these strings to an
              Excel file. After the Excel file is saved, \t becomes a space and \"
              becomes ". Then when my program later reads the strings from the Excel
              file, it doesn't think the strings are the same as those in the XML
              file.

              That is why I was trying to modify the strings from the XML file by
              getting rid of the backslashes - try to do the same thing that Excel
              does, but could not find a way to do so.

              Thanks

              Bruce Wood wrote:[color=blue]
              > In the line of code you wrote:
              >
              > string c = "'abc' \"cde\", 'mno' \"xyz\",";
              >
              > there are no backslashes in the string. The backslashes you see are
              > only a typographical convention that tells the compiler that the
              > double-quote that follows each backslash is not the double-quote that
              > ends the string, but that it should instead place a double-quote
              > character in the string.
              >
              > The string stored internally after compilation looks like this:
              >
              > 'abc' "cde", 'mno' "xyz",
              >
              > What are you trying to do in your program and what problems are you
              > encountering, since backslashes in the string don't appear to be[/color]
              what's[color=blue]
              > wrong?[/color]

              Comment

              • Etu

                #8
                Re: replace

                Thanks SerhaD. However, I tried your code but could not get what I
                want.

                Thanks

                SerhaD Inanli wrote:[color=blue]
                > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
                twice[color=blue]
                >
                > That's All
                >
                > Hopes This Helps
                >
                > "Etu" <etu_sho@yahoo. com> wrote in message
                > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=green]
                > > Hi,
                > >
                > > I have a string:
                > >
                > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                > >
                > > how can I use the c.Replace(???, ???) method to have this string:
                > >
                > > "'abc' "cde", 'mno' "xyz","
                > > that is, all the backslashes are removed.
                > >
                > > Thanks,
                > >
                > > Etu
                > >[/color][/color]

                Comment

                • Champika Nirosh

                  #9
                  Re: replace


                  "SerhaD Inanli" <serhadinanli@h otmail.com> wrote in message
                  news:eVo2nIMCFH A.3696@TK2MSFTN GP14.phx.gbl...[color=blue]
                  > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                  > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
                  twice

                  Yes but will this make any difference... to the out put... c and clearString
                  both are same as you out them...???
                  [color=blue]
                  >
                  > That's All
                  >
                  > Hopes This Helps
                  >
                  > "Etu" <etu_sho@yahoo. com> wrote in message
                  > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=green]
                  > > Hi,
                  > >
                  > > I have a string:
                  > >
                  > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                  > >
                  > > how can I use the c.Replace(???, ???) method to have this string:
                  > >
                  > > "'abc' "cde", 'mno' "xyz","
                  > > that is, all the backslashes are removed.
                  > >
                  > > Thanks,
                  > >
                  > > Etu
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • SerhaD Inanli

                    #10
                    Re: replace

                    private void RegEx_Click(obj ect sender, System.EventArg s e)

                    {

                    string c = "'abc' \"cde\", 'mno' \"xyz\",";

                    try

                    {

                    textBox1.Text = c.Replace('\\', ' '); // Second parameter is a blank char

                    }

                    catch(Exception ex)

                    {


                    MessageBox.Show (ex.Message);

                    }

                    }

                    and Result is = 'abc' "cde", 'mno' "xyz",

                    I Tried several times , do u get different output or Error ?



                    "Etu" <etu_sho@yahoo. com> wrote in message
                    news:1107323614 .789180.110010@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                    > Thanks SerhaD. However, I tried your code but could not get what I
                    > want.
                    >
                    > Thanks
                    >
                    > SerhaD Inanli wrote:[color=green]
                    > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                    > > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
                    > twice[color=green]
                    > >
                    > > That's All
                    > >
                    > > Hopes This Helps
                    > >
                    > > "Etu" <etu_sho@yahoo. com> wrote in message
                    > > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=darkred]
                    > > > Hi,
                    > > >
                    > > > I have a string:
                    > > >
                    > > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                    > > >
                    > > > how can I use the c.Replace(???, ???) method to have this string:
                    > > >
                    > > > "'abc' "cde", 'mno' "xyz","
                    > > > that is, all the backslashes are removed.
                    > > >
                    > > > Thanks,
                    > > >
                    > > > Etu
                    > > >[/color][/color]
                    >[/color]


                    Comment

                    • SerhaD Inanli

                      #11
                      Re: replace

                      private void RegEx_Click(obj ect sender, System.EventArg s e)

                      {

                      string c = "'abc' \"cde\", 'mno' \"xyz\",";

                      try

                      {

                      textBox1.Text = c.Replace('\\', ' '); // Second parameter is a blank char

                      }

                      catch(Exception ex)

                      {


                      MessageBox.Show (ex.Message);

                      }

                      }

                      and Result is = 'abc' "cde", 'mno' "xyz",

                      I Tried several times , do u get different output or Error ?



                      "Etu" <etu_sho@yahoo. com> wrote in message
                      news:1107323614 .789180.110010@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                      > Thanks SerhaD. However, I tried your code but could not get what I
                      > want.
                      >
                      > Thanks
                      >
                      > SerhaD Inanli wrote:[color=green]
                      > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                      > > string clearString = c.Replace('\\', ' '); // You must type backslash[/color]
                      > twice[color=green]
                      > >
                      > > That's All
                      > >
                      > > Hopes This Helps
                      > >
                      > > "Etu" <etu_sho@yahoo. com> wrote in message
                      > > news:1107302230 .002872.282400@ f14g2000cwb.goo glegroups.com.. .[color=darkred]
                      > > > Hi,
                      > > >
                      > > > I have a string:
                      > > >
                      > > > string c = "'abc' \"cde\", 'mno' \"xyz\",";
                      > > >
                      > > > how can I use the c.Replace(???, ???) method to have this string:
                      > > >
                      > > > "'abc' "cde", 'mno' "xyz","
                      > > > that is, all the backslashes are removed.
                      > > >
                      > > > Thanks,
                      > > >
                      > > > Etu
                      > > >[/color][/color]
                      >[/color]


                      Comment

                      • Bob Grommes

                        #12
                        Re: replace

                        Etu,

                        Why do you think that your C# program doesn't think the strings are the same
                        as those in the XML file?

                        If you are watching this in the debugger then you will see tabs replaced
                        with \t, quotes replaced with \" but this is just a display convention. The
                        debugger wants to display all strings like C# constants, and a C# constant
                        can't contain a quote, so it'll be changed to \" to be syntactially correct.
                        And any escapable characters will be transformed to escape sequences so that
                        you can see they are there.

                        If you are reading the XML files and they don't have the backslashes then C#
                        won't magically add them back in.

                        In a similar vein, why do you think that Excel changes tabs to spaces? It
                        might be doing so, particularly if it doesn't understand \t but realizes
                        it's some kind of escape sequence and changes it to a space; or, it may know
                        it's a tab but disallows or transforms them in that context. But if you are
                        going by what your eye sees to draw this conclusion then you may be drawing
                        the wrong conclusion. It may well *be* a tab.

                        --Bob

                        "Etu" <etu_sho@yahoo. com> wrote in message
                        news:1107323506 .911917.227880@ l41g2000cwc.goo glegroups.com.. .
                        [color=blue]
                        > The problem I have is actually my program first reads some strings
                        > (with \t, \" and so on) from an XML file and writes these strings to an
                        > Excel file. After the Excel file is saved, \t becomes a space and \"
                        > becomes ". Then when my program later reads the strings from the Excel
                        > file, it doesn't think the strings are the same as those in the XML
                        > file.[/color]


                        Comment

                        • Bruce Wood

                          #13
                          Re: replace

                          Ahhh... so the string you have was read from a file, not compiled into
                          your program. That makes all of the difference in the world. If the
                          string is read from a file, it really does have backslashes in it. If
                          it was compiled into your program, it doesn't.

                          Evidently, Excel processes the string during input and converts the
                          escape sequences (\", \t, \n, and the like) into their corresponding
                          characters (", tab, and newline, respectively).

                          I don't know of any built-in .NET method that will this processing for
                          you (take a string containing escape sequences and translate them to
                          their character equivalents). To do a 100% job would be a huge
                          challenge, but you can do the most common sequences (and perhaps all of
                          the ones you need) like this:

                          string cleanString;
                          cleanString = stringFromXmlFi le.Replace("\\\ "", "\"").Replace(" \\t",
                          "\t").Replace(" \\n", "\n");

                          You can add as many "replace" calls as you need to do the job.

                          The first Replace call is different because it involves the
                          double-quote, which is also the string delimeter in C#, so it needs
                          some special help. This replace call says, "Replace every occurrence of
                          a backslash (\\, because a \ by itself is an escape, so two \\
                          represent a single \ to the compiler) followed by a double-quote (\"
                          because you can't just say " or that would end the string) to a
                          double-quote."

                          The subsequent Replace calls are simpler. They say, "Replace every
                          occurrence of a backslash (\\ again) followed by (for example) an en by
                          (for example) the character \n." The compiler translates \n into the
                          newline character, so the Replace ends up replacing every two-character
                          sequence \n by the new line character.

                          You can do the same for tab (\t), return (\r), and a bunch of other
                          characters. See here for the common ones:



                          (watch for line wrapping on the URL).

                          Mind you, this won't trap all possible values: you can also use the \
                          escape character in C# followed by a hexadecimal or Unicode escape
                          sequence, which could indicate any character in the massive Unicode
                          character space. This Replace trick won't work in that case. I'll leave
                          it to someone smarter than I am to come up with a solution for that
                          one.

                          However, if you don't have any backslashes followed by numeric
                          sequences in your XML code, then you're probably fine with the trick I
                          outlined above.

                          Comment

                          • Etu

                            #14
                            Re: replace

                            Thanks a lot!

                            Etu

                            Comment

                            • Etu

                              #15
                              Re: replace

                              I appreciate your help, Bob. I jumped to the wrong conclusion but
                              later found that the reason my program doesn't "think" the two strings
                              are the same because the first apostrophe was not saved to the Excel
                              file!

                              Etu

                              Comment

                              Working...