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. Other Discussions
  3. The Weird and The Wonderful
  4. Insert a quote into a @-quoted string literal [modified]

Insert a quote into a @-quoted string literal [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
8 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.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    It's not too horrible, but while perusing an article I noticed this method of inserting quotes into a @-quoted string literal (text disguised to protect the guilty): string x = @"Blah blah blah " + **Convert.ToChar(34).ToString()** + @" etc. " ; I don't know why the author used @-quoted string literals in this case anyway. -- modified at 16:27 Monday 18th June, 2007

    M S D F 4 Replies Last reply
    0
    • P PIEBALDconsult

      It's not too horrible, but while perusing an article I noticed this method of inserting quotes into a @-quoted string literal (text disguised to protect the guilty): string x = @"Blah blah blah " + **Convert.ToChar(34).ToString()** + @" etc. " ; I don't know why the author used @-quoted string literals in this case anyway. -- modified at 16:27 Monday 18th June, 2007

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

      @-quoted literals are useful for two reasons: 1) when dealing with strings that contain backslashes; 2) when dealing with strings that contain CRLF characters. We've used them in scenario 2 for complicated queries to SQL Server CE, which does not support any form of stored program. Heck, a CREATE TABLE statement rapidly runs off the end of even the largest sensible screen width. You waste a few bytes of your program space on unnecessary spaces and tabs required to keep it all in line, but you don't keep having to concatenate. Generally strings containing backslashes are file paths. Double-quote characters aren't valid in file paths. They're valid in SQL but only for delimiting identifiers, and the square brackets [] do the same job whether or not the quoted_identifier option is on. The proper way to insert double-quotes into an @-quoted string literal in C# is of course just to double them up.

      Stability. What an interesting concept. -- Chris Maunder

      P D 2 Replies Last reply
      0
      • M Mike Dimmick

        @-quoted literals are useful for two reasons: 1) when dealing with strings that contain backslashes; 2) when dealing with strings that contain CRLF characters. We've used them in scenario 2 for complicated queries to SQL Server CE, which does not support any form of stored program. Heck, a CREATE TABLE statement rapidly runs off the end of even the largest sensible screen width. You waste a few bytes of your program space on unnecessary spaces and tabs required to keep it all in line, but you don't keep having to concatenate. Generally strings containing backslashes are file paths. Double-quote characters aren't valid in file paths. They're valid in SQL but only for delimiting identifiers, and the square brackets [] do the same job whether or not the quoted_identifier option is on. The proper way to insert double-quotes into an @-quoted string literal in C# is of course just to double them up.

        Stability. What an interesting concept. -- Chris Maunder

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Exactly, but the author didn't use the @-quoted string literal to do any of those things; no special characters, and still broke the string into parts and concatenated the parts. I use them for SQL statements and scripts. They can also be useful with regular expressions. But if you're going to use them, use them.

        1 Reply Last reply
        0
        • P PIEBALDconsult

          It's not too horrible, but while perusing an article I noticed this method of inserting quotes into a @-quoted string literal (text disguised to protect the guilty): string x = @"Blah blah blah " + **Convert.ToChar(34).ToString()** + @" etc. " ; I don't know why the author used @-quoted string literals in this case anyway. -- modified at 16:27 Monday 18th June, 2007

          S Offline
          S Offline
          Sylvester george
          wrote on last edited by
          #4

          :confused::confused::confused:

          Regards, Sylvester G sylvester_g_m@yahoo.com

          1 Reply Last reply
          0
          • M Mike Dimmick

            @-quoted literals are useful for two reasons: 1) when dealing with strings that contain backslashes; 2) when dealing with strings that contain CRLF characters. We've used them in scenario 2 for complicated queries to SQL Server CE, which does not support any form of stored program. Heck, a CREATE TABLE statement rapidly runs off the end of even the largest sensible screen width. You waste a few bytes of your program space on unnecessary spaces and tabs required to keep it all in line, but you don't keep having to concatenate. Generally strings containing backslashes are file paths. Double-quote characters aren't valid in file paths. They're valid in SQL but only for delimiting identifiers, and the square brackets [] do the same job whether or not the quoted_identifier option is on. The proper way to insert double-quotes into an @-quoted string literal in C# is of course just to double them up.

            Stability. What an interesting concept. -- Chris Maunder

            D Offline
            D Offline
            Dario Solera
            wrote on last edited by
            #5

            I remember reading somewhere that .NET compilers optimize static string concatenation. But maybe I'm wrong.

            If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

            D 1 Reply Last reply
            0
            • D Dario Solera

              I remember reading somewhere that .NET compilers optimize static string concatenation. But maybe I'm wrong.

              If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #6

              But this is not a static string concatenation because Convert.ToChar(34).ToString() isn't a constant, but a method call. Only string concatenations that don't cause a compile error in const declarations are optimized by the compiler. E.g. const string name = "Bob"; const string test = "Hello " + name; // valid, concatenation happens at compile time const string test2 = "Hello" + Convert.ToChar(32).ToString() + "Bob"; // invalid

              1 Reply Last reply
              0
              • P PIEBALDconsult

                It's not too horrible, but while perusing an article I noticed this method of inserting quotes into a @-quoted string literal (text disguised to protect the guilty): string x = @"Blah blah blah " + **Convert.ToChar(34).ToString()** + @" etc. " ; I don't know why the author used @-quoted string literals in this case anyway. -- modified at 16:27 Monday 18th June, 2007

                D Offline
                D Offline
                DynV
                wrote on last edited by
                #7

                He most probably didn't understand what he was doing, that's sad.

                Je vous salue du Québec !

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  It's not too horrible, but while perusing an article I noticed this method of inserting quotes into a @-quoted string literal (text disguised to protect the guilty): string x = @"Blah blah blah " + **Convert.ToChar(34).ToString()** + @" etc. " ; I don't know why the author used @-quoted string literals in this case anyway. -- modified at 16:27 Monday 18th June, 2007

                  F Offline
                  F Offline
                  Fuad Bin Omar
                  wrote on last edited by
                  #8

                  Placing an "@" does not produce any error for line break. e.g. the following code would produce an error. string x = "Blah blah blah" + Convert.Tochar(37) .ToString(); But the following would not string x = @"Blah blah blah" + Convert.Tochar(37) .ToString(); (I am writing this without testing. So feel free to let me know if I am wrong.)

                  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