3

I have a string with multiple lines, each line contains number of Quotation mark ("),these Quotations are too much in my string and some of them even I cant skip with back slash() also. I tried to use 31d party website ( click here ) which will do Escapeing but its reforming the lines which I need my string in its line format so I can regex them line by line. Question is there any way to skip all these quotations in easy way?

bellow is mock of my string

var stringHolder = @" book book "book"
ten ten "book" book pen
pen "hook book" dook
beer poor "111" cat map"

Tnx alot in advance

  • 1
    English is not my first language, but when you say "skip" you mean "escape" ? – cantSleepNow Dec 12 '18 at 21:17
  • @cantSleepNow soryy, its not my mother lanquage too, I corrected. tnx –  Dec 12 '18 at 21:20
  • 1
    What are you trying to accomplish? In C# if you use a [verbatim string literal](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim) then you must double up `"` marks inside it. – NetMage Dec 12 '18 at 21:20
  • how can I see the other question ? so I can compare it with mine @NetMage –  Dec 13 '18 at 22:14
  • It is listed at the top of your question when you refresh. – NetMage Dec 13 '18 at 22:59

1 Answers1

9

To escape double quotes in a verbatim string (ie, a string declared with the @ prefix), simply double the quotes up (""). This is in contrast to how you would normally escape a double quote in a string, \".

var stringHolder = @" book book ""book""
ten ten ""book"" book pen
pen ""hook book"" dook
beer poor ""111"" cat map";
Console.WriteLine(stringHolder);
/*Output:
 book book "book"
ten ten "book" book pen
pen "hook book" dook
beer poor "111" cat map
*/

When the indentation matters, you may have to fight the normal tabbing in your editor a bit, which can lead to some odd looking declarations.

namespace MyNamespace {
    public class Foo {
        public string GetString() => @"Hello
World"; // Returns a string that looks like
        // Hello
        // World

        public string GetString2() => @"Hello
            World"; // Returns a string that looks like
                    // Hello
                    //             World
    }
}

While this feature is currently in preview for C# 11, the following way of declaring multi-line strings that contain quotes is likely to be available in the coming future.

var stringHolder = """
     book book "book"
    ten ten "book" book pen
    pen "hook book" dook
    beer poor "111" cat map
    """;
    
    Console.WriteLine(stringHolder);
/*Output:
 book book "book"
ten ten "book" book pen
pen "hook book" dook
beer poor "111" cat map
*/

At the moment, the rules for how many quotes (in this example, 3) that are required is that the header and footer quotes be at least 3, or N+1 the amount of consecutive quotes expected in the string.

Jonathon Chase
  • 9,396
  • 21
  • 39