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. @-quoted strings

@-quoted strings

Scheduled Pinned Locked Moved C#
csharpdebuggingxmlhelptutorial
9 Posts 4 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.
  • U Offline
    U Offline
    User 1180270
    wrote on last edited by
    #1

    I've got a slight problem with @-quoted strings in C#. I'm reading some XML formatted data using a XmlTextReader. At some point in this process I get a chunk of text that contains some escape sequences. I want to use this as the format part in an StringBuilder.AppendFormat() method. StringBuilder sb = new StringBuilder() ; string format = myXmlTextReader.Value ; sb.AppendFormat(format, ...) ; However the value in format is @-quoted. If I look in the debugger it shows the following: format = @"The user says\n{0}" I just can't work out how to un @-quote the string in code. :confused: I want the newline in the string not the \n. There must be a simple way to do this but I just can't find it! TIA Jackson

    C S P 3 Replies Last reply
    0
    • U User 1180270

      I've got a slight problem with @-quoted strings in C#. I'm reading some XML formatted data using a XmlTextReader. At some point in this process I get a chunk of text that contains some escape sequences. I want to use this as the format part in an StringBuilder.AppendFormat() method. StringBuilder sb = new StringBuilder() ; string format = myXmlTextReader.Value ; sb.AppendFormat(format, ...) ; However the value in format is @-quoted. If I look in the debugger it shows the following: format = @"The user says\n{0}" I just can't work out how to un @-quote the string in code. :confused: I want the newline in the string not the \n. There must be a simple way to do this but I just can't find it! TIA Jackson

      C Offline
      C Offline
      Carsten Zeumer
      wrote on last edited by
      #2

      Er... it IS unquoted. It is only displayed as verbatim string (otherwise it would contain a CR that wóuld not look so nice in the IDE's watch window). Do not use the debugger but try a Console.Out.WriteLine(format); to verify my claim. /cadi 24 hours is not enough

      1 Reply Last reply
      0
      • U User 1180270

        I've got a slight problem with @-quoted strings in C#. I'm reading some XML formatted data using a XmlTextReader. At some point in this process I get a chunk of text that contains some escape sequences. I want to use this as the format part in an StringBuilder.AppendFormat() method. StringBuilder sb = new StringBuilder() ; string format = myXmlTextReader.Value ; sb.AppendFormat(format, ...) ; However the value in format is @-quoted. If I look in the debugger it shows the following: format = @"The user says\n{0}" I just can't work out how to un @-quote the string in code. :confused: I want the newline in the string not the \n. There must be a simple way to do this but I just can't find it! TIA Jackson

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        The debugger always shows all strings verbatim. Try printing to the console and see if it works correctly. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        U 1 Reply Last reply
        0
        • S S Senthil Kumar

          The debugger always shows all strings verbatim. Try printing to the console and see if it works correctly. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          U Offline
          U Offline
          User 1180270
          wrote on last edited by
          #4

          Well - I've added the following two lines to the app: System.Console.WriteLine(format); System.Console.WriteLine("test\nstring"); And this produces the following in the Output Window: The user says\n{0} test string Just to confuse things further before I display the final string from the StringBuilder I looked at the value in the string and the debugger shows the following: msg="Test Dynamic Form\nLess than 20 users impacted\nNo you can't\nNO VALUE SELECTEDThe user says\\nTest" The debugger does not show the @ for a @-quoted string this time and the final \n has been escaped! It isn't escaped when its in the variable format - just to be sure i replaced all occurances of \ with ! and printed out the result. So - still confused here:-D

          S 1 Reply Last reply
          0
          • U User 1180270

            Well - I've added the following two lines to the app: System.Console.WriteLine(format); System.Console.WriteLine("test\nstring"); And this produces the following in the Output Window: The user says\n{0} test string Just to confuse things further before I display the final string from the StringBuilder I looked at the value in the string and the debugger shows the following: msg="Test Dynamic Form\nLess than 20 users impacted\nNo you can't\nNO VALUE SELECTEDThe user says\\nTest" The debugger does not show the @ for a @-quoted string this time and the final \n has been escaped! It isn't escaped when its in the variable format - just to be sure i replaced all occurances of \ with ! and printed out the result. So - still confused here:-D

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Oops. My bad. The debugger doesn't show all strings verbatim, it shows them only for 1. those that have the @ and have escaped characters, for eg, @"senthil\n". 2. those that don't have an @ but still have escaped characters, like "senthil\\". "\n", "\t" seem to be exceptions to this rule. Anyway, your problem is because format has a literal \n in it, that is, format = @"The user says \n". You'd need to search for the literal \n ("\\n" or @"\n") and replace it with the line breaking character. In short

            string x = @"senthil\n";
            string y =x.Replace(@"\n", "\n");
            Console.WriteLine(y);

            will do what you want. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            U 1 Reply Last reply
            0
            • S S Senthil Kumar

              Oops. My bad. The debugger doesn't show all strings verbatim, it shows them only for 1. those that have the @ and have escaped characters, for eg, @"senthil\n". 2. those that don't have an @ but still have escaped characters, like "senthil\\". "\n", "\t" seem to be exceptions to this rule. Anyway, your problem is because format has a literal \n in it, that is, format = @"The user says \n". You'd need to search for the literal \n ("\\n" or @"\n") and replace it with the line breaking character. In short

              string x = @"senthil\n";
              string y =x.Replace(@"\n", "\n");
              Console.WriteLine(y);

              will do what you want. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              U Offline
              U Offline
              User 1180270
              wrote on last edited by
              #6

              Senthil, Yes - this does indeed solve the problems for newlines, and is the route I had started down. However the issue with this approach is the I have to code up a method that knows about all of the escape codes and transforms them. I was hoping to find a method that didn't require that I capture this information in code. However I can't think of anything better :) and it should be possible to write a big regexp that covers of the escape sequences I want to allow. Thanks for the help Jackson

              S 1 Reply Last reply
              0
              • U User 1180270

                Senthil, Yes - this does indeed solve the problems for newlines, and is the route I had started down. However the issue with this approach is the I have to code up a method that knows about all of the escape codes and transforms them. I was hoping to find a method that didn't require that I capture this information in code. However I can't think of anything better :) and it should be possible to write a big regexp that covers of the escape sequences I want to allow. Thanks for the help Jackson

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                I don't think XML data that is read can contain linebreaks, as the XML parser ignores linebreaks and spaces between tags (NOT for attribute values though). So I think you have to anway convert literal \n s to linebreaks anyway. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                U 1 Reply Last reply
                0
                • S S Senthil Kumar

                  I don't think XML data that is read can contain linebreaks, as the XML parser ignores linebreaks and spaces between tags (NOT for attribute values though). So I think you have to anway convert literal \n s to linebreaks anyway. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  U Offline
                  U Offline
                  User 1180270
                  wrote on last edited by
                  #8

                  I think your right. Thanks Jackson

                  1 Reply Last reply
                  0
                  • U User 1180270

                    I've got a slight problem with @-quoted strings in C#. I'm reading some XML formatted data using a XmlTextReader. At some point in this process I get a chunk of text that contains some escape sequences. I want to use this as the format part in an StringBuilder.AppendFormat() method. StringBuilder sb = new StringBuilder() ; string format = myXmlTextReader.Value ; sb.AppendFormat(format, ...) ; However the value in format is @-quoted. If I look in the debugger it shows the following: format = @"The user says\n{0}" I just can't work out how to un @-quote the string in code. :confused: I want the newline in the string not the \n. There must be a simple way to do this but I just can't find it! TIA Jackson

                    P Offline
                    P Offline
                    Pyro Joe
                    wrote on last edited by
                    #9

                    I'm not sure I completely understand your problem. However, I gather you simply find it a nuisance to have to write an extra / mark in your code. I made a little utility a while ago, that if you paste any text you want to send to a textbox, you simply paste or enter the text you want formatted. Then click process, and it inserts all the slash marks, and etc. Then you paste that code into your application, and voilla, you can have the text programmatically inserted into a textbox or richTextBox. idk if it's useful or not, but you can download it here (I find it handy for entering multiple lines of code): http://www.teched.coe.ohio-state.edu/lugnut/stephen/Text Formatting Revealer.zip if it helps you any let me know. I'm aware the output is somewhat sloppy, nevertheless it works with 100% functionality as far as I've tested it. good luck solving your problem, Stephen

                    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