0

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.

MatthewD
  • 6,719
  • 5
  • 22
  • 41
  • So you want to concatenate `strPrefix` with `strLastPath` but add a `"` in the middle? – DavidG Oct 09 '15 at 16:34
  • Yes sir. I am adding a space and need the double quote after the space next to the strLastPath value. – MatthewD Oct 09 '15 at 16:35
  • @MatthewD you could have found this by doing a simple google search for example open up a new browser and type the following in the search `C# stackoverflow add double quotes around a string variable` – MethodMan Oct 09 '15 at 16:36

3 Answers3

2

You have two options:

var output1 = strPrefix + "\"" + strLastPath;

Or using a verbatim string:

var output2 = strPrefix + @"""" + strLastPath;
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

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);
Rob
  • 45,296
  • 24
  • 122
  • 150
  • I know I've tried that but let me give it a try – MatthewD Oct 09 '15 at 16:37
  • If you've tried it then either I've missed the point of what you're trying to achieve, or you tried it wrong .... as I have a console window open right now showing the text you're expecting to see! =) – Rob Oct 09 '15 at 16:38
  • That is it. Man I know i tried that. – MatthewD Oct 09 '15 at 16:38
  • You know what it is, I am seeing the \" in the immediate windows but when the sting is written it doesn't come out. – MatthewD Oct 09 '15 at 16:39
1

Please try this

     var strPrefix = "convert -density 150";

        var strLastPath = @"L:\03 Supervisors\";

        Console.WriteLine(strPrefix + " " + '"'+strLastPath);
Kapoor
  • 1,388
  • 11
  • 21