首页 > 解决方案 > 删除 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());

有什么办法吗?

我将不胜感激任何帮助。

标签: c++stringcmdcommand

解决方案


如果您想删除所有出现的字符,那么您可以使用

#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); 

推荐阅读