“|” c# 字符串中的运算符 - 如何转义它?
"|" Operator inside c# string - how to escape it?
我有以下字符串 - 这是执行 exe 的参数。
但我收到错误消息 - "Operator "|" 不能应用于字符串和字符串类型的操作数。
我怎么转义我的“|” ?我试过\,没用:(
string.Format(
@"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t "|" -r \r\n -rt value -rv 1000 -W -fh 0",
saveFilePath + a,
saveFilePath + b);
在 @"..."
文字中,要有一个 "
字符,您必须使用其中两个。所以:
string.Format(
@"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t ""|"" -r \r\n -rt value -rv 1000 -W -fh 0",
// note -----------------------------------------------------------------^^-^^
saveFilePath + a,
saveFilePath + b);
但是,如果你想让那些\r
和\n
成为回车符return和换行符,你不能使用@"..."
字符串文字,因为反斜杠在它们中并不特殊(这就是它们的全部意义)。所以如果是这样的话:
string.Format(
"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t \"|\" -r \r\n -rt value -rv 1000 -W -fh 0",
// note ^-------------------------------------------------------------------^^-^^
saveFilePath + a,
saveFilePath + b);
推荐阅读: string (C# Reference)
旁注:您不需要对函数参数进行字符串连接。由于您已经呼叫string.Format
,您可以让它这样做:
string.Format(
"-S -E -M -m -e ascii -i {0}{1}.dat -T db1.dbo.table1 -R {2}{3}.reject -t \"|\" -r \r\n -rt value -rv 1000 -W -fh 0",
// note ---------------------^^^^^^--------------------------^^^^^^
saveFilePath,
a, // <==
saveFilePath,
b); // <==
我有以下字符串 - 这是执行 exe 的参数。
但我收到错误消息 - "Operator "|" 不能应用于字符串和字符串类型的操作数。
我怎么转义我的“|” ?我试过\,没用:(
string.Format(
@"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t "|" -r \r\n -rt value -rv 1000 -W -fh 0",
saveFilePath + a,
saveFilePath + b);
在 @"..."
文字中,要有一个 "
字符,您必须使用其中两个。所以:
string.Format(
@"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t ""|"" -r \r\n -rt value -rv 1000 -W -fh 0",
// note -----------------------------------------------------------------^^-^^
saveFilePath + a,
saveFilePath + b);
但是,如果你想让那些\r
和\n
成为回车符return和换行符,你不能使用@"..."
字符串文字,因为反斜杠在它们中并不特殊(这就是它们的全部意义)。所以如果是这样的话:
string.Format(
"-S -E -M -m -e ascii -i {0}.dat -T db1.dbo.table1 -R {1}.reject -t \"|\" -r \r\n -rt value -rv 1000 -W -fh 0",
// note ^-------------------------------------------------------------------^^-^^
saveFilePath + a,
saveFilePath + b);
推荐阅读: string (C# Reference)
旁注:您不需要对函数参数进行字符串连接。由于您已经呼叫string.Format
,您可以让它这样做:
string.Format(
"-S -E -M -m -e ascii -i {0}{1}.dat -T db1.dbo.table1 -R {2}{3}.reject -t \"|\" -r \r\n -rt value -rv 1000 -W -fh 0",
// note ---------------------^^^^^^--------------------------^^^^^^
saveFilePath,
a, // <==
saveFilePath,
b); // <==