c++-cli如何用“\\”替换“\”
c++-cli how to replace "\" with "\\"
我正在尝试将字符串中的所有符号替换为“\”到“\\”。但它并没有取代,我不知道为什么。尝试将 "a" 替换为 "b" 时它工作正常。代码如下:
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("\"", "\");
}
使用这个:Replace("\", "\\")
.
\
是转义字符,\
产生文字反斜杠。
(我假设您想将所有 \
替换为 \
,例如将 a
替换为 b
。)
我认为您的代码中有错字。
您的意思是将单个“\”字符更改为两个“\”字符吗??
尝试替换("\", "\\");
您需要使用“\”来表示单个“\”字符 - 因此需要使用 4 个字符来指定 2 个“\”字符。
我正在尝试将字符串中的所有符号替换为“\”到“\\”。但它并没有取代,我不知道为什么。尝试将 "a" 替换为 "b" 时它工作正常。代码如下:
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("\"", "\");
}
使用这个:Replace("\", "\\")
.
\
是转义字符,\
产生文字反斜杠。
(我假设您想将所有 \
替换为 \
,例如将 a
替换为 b
。)
我认为您的代码中有错字。
您的意思是将单个“\”字符更改为两个“\”字符吗??
尝试替换("\", "\\");
您需要使用“\”来表示单个“\”字符 - 因此需要使用 4 个字符来指定 2 个“\”字符。