-2

I have following string - which is an argument to execute an exe.

But I am getting error - "Operator "|" can not be applied to operand of type string and string.

Ho do I escape my "|" ? I tried \, didnt work :(

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);
Relativity
  • 6,690
  • 22
  • 78
  • 128

1 Answers1

4

In a @"..." literal, to have a " character, you must use two of them. So:

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);

However, if you wanted those \r and \n to be a carriage return and a newline, you can't use a @"..." string literal because backslash isn't special in them (that's the whole point of them). So if that's the case:

    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);

Recommended reading: string (C# Reference)


Side note: There's no need for the string concatenation you're doing on the function arguments. Since you're already calling string.Format, you can have it do that:

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);            // <==
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875