c++ - 删除 std::string 上的反斜杠字符时出现问题
问题描述
我正在尝试执行正在反序列化 JSON 消息的 CMD 命令。
当我反序列化消息时,我将值存储在一个std::string
变量中,其值为"tzutil /s \"Romance Standard Time_dstoff\""
:
当我收到带有浮动引号参数(例如)的命令时,我想删除反斜杠字符('\' "tzutil /s "Romance Standard Time_dstoff""
)。
std::string command = "tzutil /s \"Romance Standard Time_dstoff\""; //Problem
system(command.c_str());
有什么办法吗?
我将不胜感激任何帮助。
解决方案
如果您想删除所有出现的字符,那么您可以使用
#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), char_to_remove), str.end());
如果您想用另一个字符替换它们,请尝试
#include <algorithm>
std::replace(str.begin(), str.end(), old_char, new_char);
推荐阅读
- react-native - 我们可以将现有的 React Native 应用程序转换为 Flutter,而无需在 Flutter 中从头开始编写代码吗
- opengl - 在 Opengl 中修改 VBO 的最佳方法是什么?喜欢推流行吗?
- javascript - 禁用新日期时间的前 7 周并禁用日期选择器的周末
- mysql - Mysql JDBC getTimestamp 返回错误值
- python - 在 GCP 中使用 tensorflow_cloud 训练模型时如何引用自定义 Python 文件?
- python - 是否可以将 Tkinter 中的图像移到最后面?
- qt - QT 样式表中的混合背景颜色
- mysql - 为什么更新 onetomany 字段会使 typeorm 中的所有其他先前归档为空
- linux - 无法在 Parrot OS 上执行 apt 更新
- vb.net - VB.Net Await 抛出 NullReferenceException