Search code examples
c#stringstring-concatenationdouble-quotes

Building string add double quote between two variables


I have been looking at this for a while trying everything I can find on SO. There are many posts but I must be missing something.

I am building a string and need to get a double quote in it between two variables.

I want the string to be

convert -density 150 "L:\03 Supervisors\

The code is

tw.WriteLine(strPrefix + " " + strLastPath);

where

strPrefix = convert -density 150

and

strLastPath = L:\\03 Supervisors\

I have tried many combinations of "" """ """" \" "\ in the middle where the space is being inserted.

Please show me what I am missing.


Solution

  • Here's a sample console application that achieves what you're after:

    namespace DoubleQuote
    {
        class Program
        {
            static void Main(string[] args)
            {
                var strPrefix = "convert - density 150";
                var strLastPath = @"L:\\03 Supervisors\";
    
                Console.WriteLine(strPrefix + " \"" + strLastPath);
    
                Console.ReadKey();
            }
        }
    }
    

    If written as a format string it would look like this:

    var textToOutput = string.Format("{0} \"{1}", strPrefix, strLastPath);
    Console.WriteLine(textToOutput);