【发布时间】:2019-01-31 23:03:34
【问题描述】:
我需要生成一个可以匹配另一个既包含特殊字符的字符串。我写了我认为是一个简单的方法,但到目前为止还没有一个成功的匹配。
我知道 c++ 中的特殊字符以“\”开头。例如,单引号应写为“\'”。
string json_string(const string& incoming_str)
{
string str = "\\\"" + incoming_str + "\\\"";
return str;
}
这是我必须比较的字符串:
bool comp = json_string("hello world") == "\"hello world\"";
我可以在 cout 流中看到,实际上我正在根据需要生成字符串,但比较仍然给出 false 值。
我错过了什么?任何帮助将不胜感激。
【问题讨论】:
-
字符串
"\"hello world\""不等于"\\\"hello world\\\""。 -
"\\\"" + incoming_str + "\\\""变成\"incoming_str\",而"\"hello world\""只变成"hello world"。而不是string str = "\\\"" + incoming_str + "\\\"";尝试string str = "\"" + incoming_str + "\"";