如何在不同的标志切断绳子

How to cut string at various signs

本文关键字:标志      更新时间:2023-10-16

我有以下问题:我从。csv文件中读取WinCC变量。现在有一个包含ip地址的字符串。它看起来像这样:I0043CTRL/CALH1$ST$Beh$stVal;Len=4;MMSType=133;Flag=RW

本例中Address为I0043

现在我想截断地址后面的字符串,但是变量的名称有更多可能,例如I0043PROT/...

是否有可能告诉例如getline结束在不同的标志?如:getline(tmp_stringstream,tmp_string, 'C' || 'P');

谢谢帕特里克

boost::split做你需要的:http://www.boost.org/doc/libs/1_53_0/doc/html/string_algo/usage.html#idp163440592

std::string mystring("asd,ff.erw qewr");
std::vector<std::string> tokens;
boost::split( tokens, mystring, boost::is_any_of(",.-/ ") );

在C运行库中有一个字符串标记器函数,strtok(包括)

在c++运行时中有一个等效的std::strtok(包括<装运箱>)

您可以使用std::string::find_first_ofstd::string::substr:

string line("I0043CTRL/CALH1$ST$Beh$stVal;Len=4;MMSType=133;Flag=RW");
cout << line.substr(0, line.find_first_of("CP"));
输出:

I0043