c++ - 剪切带有多个结束符的字符串

标签 c++ string split substring standard-library

我正在尝试找出两个不同字符之间的子字符串。该字符串看起来像:

channelBlock_0\d_off_mux=8, 8, 8, 8, 8, 8, 8, 8
channelBlock_0\d_selvth=true, true, true, true, true, true, true, true

我想在 '=' 之后和第一个 ',' 之前进行拆分

'='之后进行切割已经对我有用......这就是我得到的:

std::string line;             //contains the string from above
std::string startDel = "=";
std::string endDel   = ",";

cout << line.substr(line.find(startDel)+1,line.find(endDel));

我的输出如下所示:

8, 8, 8, 8, 8, 8, 8, 8
true, true, true, true, true

我怎样才能在第一个“,”之后剪切,这样我的输出就是

8
true

最佳答案

检查后substring() ,你可以看到你需要的是:

line.substr(line.find(startDel) + 1, line.find(endDel) - (line.find(startDel) + 1));

因为该方法的第二个参数指出:

len

Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).

关于c++ - 剪切带有多个结束符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154873/

相关文章:

javascript - 拆分 JavaScript 变量的字符串内容并分配给单独的 JS 变量

c++ - 为什么添加虚拟方法会增加 C++ 中的类大小?

objective-c - 从一位整数生成两位数的字符串

java - 搜索数组中带有空格的元素

c++ - 为什么我不能正常初始化 typedef struct string 或 char?

random - 在 JMeter 中选择随机分割变量

c++ - Boost::Python 静态链接

c++ - 创建用户定义类的实例并打开文件后出现 memset 问题

c++ - 在这种情况下,是否有与 C++03 的 constexpr 类似的东西?

java - 字符串分割并获取特定字符串