在 C++ 中用单反斜杠替换双反斜杠

问题描述 投票:0回答:1

我知道这个问题以前曾被问过,但我找到的解决方案都不适合我。

我有一个带有字符串

string s = "\"C:\\a\\b\\c\\file.dat\""
的 C++ 代码,我想用
'\\'
替换所有
'\'
并删除所有出现的
'\"'
s
的结果应该是
"C:\a\b\c\file.dat"
。基于
string::replace
regex_replace
的解决方案都不适合我。

有什么想法吗?谢谢。

c++ regex replace
1个回答
0
投票

您的字符串已经正确,但您可以使用“原始字符串文字”使代码中的字符串看起来更像输出字符串。 请参阅:原始字符串文字

#include <string>
#include <iostream>

int main()
{
    std::string s = R"(C:\a\b\c\file.dat)";
    std::cout << s;
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.