How do I convert a string to a verbatim string using its identifier

1.4k Views Asked by At

For one method I have the parameters take a string, but I need that string to become a verbatim string once in the method. Here is an example of what I mean.

//the string I have
string test = "c:\\documents\\testfile\\test.txt"

//the string I want
string converted = @"c:\\documents\\testfile\\test.txt"

how do I go about converting this using the test identifier?

I have tried:

string test = "c:\\documents\\testfile\\test.txt"

string converted = @test;

string converted = @+test;

string converted = @""+test;

Is there a way I can do this? Thanks

1

There are 1 best solutions below

0
On

You can't use the verbatim identifier like you are trying to. You'll have to do something along the lines of:

string test = "c:\\documents\\testfile\\test.txt";
string converted = test.Replace(@"\", @"\\");