简体   繁体   English

如何在各种标志处切割弦

[英]How to cut string at various signs

I´ve got the following problem: I read in WinCC Variables from a .csv file. 我遇到了以下问题:我从.csv文件中读取WinCC变量。 Now there is a string which contains the ip address. 现在有一个包含ip地址的字符串。 It looks like this: I0043CTRL/CALH1$ST$Beh$stVal;Len=4;MMSType=133;Flag=RW 它看起来像这样: I0043CTRL/CALH1$ST$Beh$stVal;Len=4;MMSType=133;Flag=RW

The Address in this example is I0043 . 此示例中的地址是I0043

Now I want to cut the string after the address, but there are more possible name of the variabel, for example I0043PROT/... . 现在我想在地址后剪切字符串,但有更多可能的变量名称,例如I0043PROT/...

Is there any possibility to tell for example getline to end at various signs? 有没有可能告诉例如getline以各种迹象结束? Like: getline(tmp_stringstream,tmp_string, 'C' || 'P'); 比如: getline(tmp_stringstream,tmp_string, 'C' || 'P');

Thank you 谢谢

Patrick 帕特里克

boost::split does what you need: http://www.boost.org/doc/libs/1_53_0/doc/html/string_algo/usage.html#idp163440592 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(",.-/ ") );

In C runtime library there's a string tokenizer function, strtok (include < string.h>) 在C运行时库中有一个字符串标记化函数, strtok (include <string.h>)

In C++ runtime there's an equivalent std::strtok (include < cstring>) 在C ++运行库中有一个等价的std :: strtok (include <cstring>)

You can use std::string::find_first_of , and std::string::substr : 您可以使用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"));

output: 输出:

I0043

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM