Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a string to the verbatim string literal

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
like image 705
heinst Avatar asked May 10 '12 15:05

heinst


2 Answers

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 (+ "\"").

like image 96
Daniel Hilgarth Avatar answered Oct 03 '22 19:10

Daniel Hilgarth


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 }";
like image 37
DeadlyChambers Avatar answered Oct 03 '22 18:10

DeadlyChambers