9

I have a path that is named defaultPath I want to add it into this verbatim string literal but can quite get the quotes around it.

    @"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\Data"""

I was trying to add +defaultPath to replace Data. So lets say I have a folder name Data.Apple I want the output to be

   "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple"

But when I have been doing it for the past half hour I have been getting

   "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\"Data.Apple

or

   "C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\" + defaultPath
lukiffer
  • 11,025
  • 8
  • 46
  • 70
heinst
  • 8,520
  • 7
  • 41
  • 77

3 Answers3

14

Do it like this (preferred):

string.Format(@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\{0}""", defaultPath);

Or like this:

@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\" + defaultPath + "\"";

The first one uses string.Format, which basically replaces the {0} in the first parameter with the value in the second parameter and returns the result.

The second one uses classical string concatenation and what I did there was to remove the double quotes after the last backslash (""..\ instead of ""..\""), because you didn't want the quotes after the backslash. You wanted the quotes after defaultPath. And that's what this code does: It appends defaultPath (" + defaultPath) and appends the closing quote afterwards (+ "\"").

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • @heinst: Updated answer. Does this make sense to you? – Daniel Hilgarth May 10 '12 at 15:51
  • I was trying to do the second answer....I just didnt add the second \....why did you add that? Other than that they both make sense – heinst May 10 '12 at 15:57
  • Why do you say it's 'preferred'? Personally, I find it a bit of a code-smell that `string.Format` is used for concatenation where it doesn't do any actual formatting. Like most things, it depends... – nicodemus13 May 10 '12 at 15:57
  • @heinst: I assume you are talking about this part: `"\""`? Would it be clearer to you, if I would have written it as `@""""` instead? This adds the closing quote after your path. Without it, the result would be `"C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple` and not `"C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple"` – Daniel Hilgarth May 10 '12 at 16:00
  • @nicodemus13: It depends, you are right. But in this example, I find the version with `string.Format` much more readable than the other one. – Daniel Hilgarth May 10 '12 at 16:03
13

So if you would like to take advantage of the string interpolation with c# 6 you could also do

var randomText = "insert something";
var yourString = $@"A bunch of text in here 
that is on seperate lines
but you want to {randomText }";
DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61
  • 4
    If you want to include literal curly brackets, e.g. when creating a JSON string, you can escape those by duplicating them just like de double quotes. Like so: `string value = "test"; string json = $@"{{""val"": ""{value}""}}";` – R. Schreurs Feb 25 '20 at 09:10
3

Use string.Format to insert the variable between the quotes:

string path = "Data.Apple";
string verbatim = string.Format(@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""{0}""", path);
MessageBox.Show(verbatim);

It makes it easier to read and to implement, you can replace other portions of the path with variable sections in a similar manner.

If you try to just append the "defaultPath" variable to the end, it will never work correctly, as you've already added the closing ".

Jason Larke
  • 5,289
  • 25
  • 28