Deividas Kiznis
Deividas Kiznis

Reputation: 451

c++-cli how to replace "\" with "\\"

I'm trying replace all symbols in a string with "\" to "\\". But it doesn't replace, and I don't know why. It works fine when trying replace "a" to "b". Code below:

 Private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

 OpenFileDialog ^ofd = gcnew OpenFileDialog();
 if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
 {
     StreamReader ^read = gcnew StreamReader(File::OpenRead(ofd->FileName));
     textBox3->Text = ofd->FileName->Replace("\"", "\\");
 }

Upvotes: 0

Views: 332

Answers (2)

lostbard
lostbard

Reputation: 5220

I think a typo in your code.

Do you mean change the single '\' character to two '\' characters ??

Try Replace("\", "\\");

You need use '\' to signify a single '\' character - so need 4 of them to specify 2 '\' characters.

Upvotes: 0

Kirill Bulygin
Kirill Bulygin

Reputation: 3826

Use this: Replace("\\", "\\\\").

\ is the escaping character, \\ produces a literal backslash.

(I assume you want to replace all \ with \\, like replacing a with b.)

Upvotes: 2

Related Questions