我有一个名为 defaultPath 的路径,我想将它添加到这个逐字字符串文字中,但可以完全得到它周围的引号。
@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\Data"""
我试图添加 +defaultPath 来替换数据。假设我有一个名为 Data.Apple 的文件夹,我希望输出为
"C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\Data.Apple"
但是当我在过去的半小时里一直这样做的时候,我得到了
"C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\"Data.Apple
或
"C:\Mavro\MavBridge\Server\MavBridgeService.exe" /service /data "..\" + defaultPath
最佳答案
这样做(首选):
string.Format(@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\{0}""", defaultPath);
或者像这样:
@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\" + defaultPath + "\"";
第一个使用string.Format
,它基本上将第一个参数中的 {0} 替换为第二个参数中的值并返回结果。
第二个使用经典的字符串连接,我所做的是删除最后一个反斜杠后的双引号(""..\
而不是 ""..\""
), 因为你不想要反斜杠后的引号。您需要 defaultPath
后的引号。这就是这段代码的作用:它附加 defaultPath
("+ defaultPath
) 并在之后附加结束引号 (+ "\""
)。
关于c# - 将字符串添加到逐字字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537578/