C++提升报价replace_all
C++ boost replace_all quote
我对boost::replace_all
有问题。我的字符串看起来像:
"日期":1481200838,"消息":">
我希望它看起来像:
"日期":1481200838,"消息":">
所以我想用单"
替换""
:
boost::replace_all(request_json_str, """", """);
但它根本不起作用。与:
boost::replace_all(request_json_str, """", """);
我怎样才能使它工作?
您需要正确转义调用中的"
字符以提升::replace_all!
// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <boost/algorithm/string/replace.hpp>
int main()
{
std::string msg(""Date"":1481200838,""Message"":"");
boost::replace_all(msg, """", """);
std::cout << msg << std::endl;
}
答案中已有的boost::replace_all(request_json_str, """", """)
是使用boost::replace_all
处理此问题的正确方法:http://coliru.stacked-crooked.com/a/af7cbc753e16cf4f
我想发布一个额外的答案,说鉴于auto request_json_str = """Date"":1481200838,""Message"":"""s
重复的引号也可以在没有 Boost 的情况下删除(尽管不是那么雄辩,使用unique
、distance
和string::resize
):
request_json_str.resize(distance(begin(request_json_str), unique(begin(request_json_str), end(request_json_str), [](const auto& a, const auto& b){ return a == '"' && b == '"'; })));
相关文章:
- 方法错误"not all control paths return a value"和方法不返回值
- 生成文件:6:目标'all'的配方在 Ubuntu-16.04 上失败
- 每次都会重建多个程序的制作文件,尽管配方"all"被标记为虚假
- 制造:对'all'错误无能为力
- 如何使用字符串::replace方法写入文件
- 为什么Eigen不能解析内置符号?(all、last、seq等)
- 如何在插入排序中使用 replace() 使语句变得不必要
- Arduino trim() 和 replace() 方法返回从 'void' 到非标量类型'String'请求的转换
- 警告"C++ requires a type specifier for all declaration"地图
- 为什么在调用 QXYSeries::replace() 后,当 size() 没有超过 capacity() 时 QVe
- 为什么 string::replace 在 C++11 中没有指定分配器的例外?
- gcc -O2 比 gcc -O2 -g 小,后跟 strip --strip-all
- 警告:"Not all control paths return a value" c++
- 如何使用 QString::replace on const& 参数?
- 为什么使用 .replace 运算符时字符串的长度不超过 16 个字符?
- C++ s.replace 函数不输出空格
- AVX2浮子比较并获得0.0或1.0,而不是all-0或全零件
- 链接时,"grab what you need" 和 "grab all" 之间是否有某些内容(-wl,--whole-archive)?
- 在C++中向枚举添加"all"选项的好设计是什么?
- 级运算符 ( "C++ requires a type specifier for all declarations" )