拆分点上的字符串,并在C ++中从中检索每个值 [英] Split the string on dot and retrieve each values from it in C++
问题描述
我需要拆分上的字符串。
I need to split the string on .
in C++..
下面是我的字符串 -
Below is my string -
@ event.hello.dc1
在上面的字符串上分割。
并从中检索@event,然后将 @event
传递给下面的方法 -
Now I need to split on .
on the above string and retrieve the @event from it and then pass @event
to the below method -
bool upsert(const char * key);
以下是我在阅读这里 -
Below is the code I have got so far after reading it from here -
void splitString() {
string sentence = "@event.hello.dc1";
istringstream iss(sentence);
copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(cout, "\n"));
}
但我不能理解如何提取 @event
通过分割在。
使用上述方法作为上述方法只适用于空格...以及如何从该字符串提取一切通过拆分。
如下所述 -
But I am not able to understand how to extract @event
by splitting on .
using the above method as the above method only works for whitespace... And also how to extract everything from that string by splitting on .
as mentioned like below -
split1 = @event
split2 = hello
split3 = dc1
/ p>
Thanks for the help..
推荐答案
您可以使用 std :: getline
:
You can use std::getline
:
string sentence = "@event.hello.dc1";
istringstream iss(sentence);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '.')) {
if (!token.empty())
tokens.push_back(token);
}
tokens[0] == "@event"
tokens[1] == "hello"
tokens[2] == "dc1"
这篇关于拆分点上的字符串,并在C ++中从中检索每个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!