【问题标题】:cut strings with multiple enddelimiter使用多个 enddelimiter 剪切字符串
【发布时间】:2018-02-19 15:31:00
【问题描述】:

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

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

【问题讨论】:

  • 您的问题是什么?顺便说一句,8 变成6 的方式令人惊讶
  • 本质上这是this的欺骗。您只需在第一次读取后将分隔符从 = 更改为 ,
  • 按照你对=的方式做。

标签: c++ string split substring standard-library


【解决方案1】:

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

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

因为方法的第二个参数声明:

len

要包含在子字符串中的字符数(如果字符串是 更短,使用尽可能多的字符)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2022-01-05
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多