I need to split the string on .
in C++..
Below is my string -
@event.hello.dc1
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"));
}
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
Thanks for the help..
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);
}
which results in:
tokens[0] == "@event"
tokens[1] == "hello"
tokens[2] == "dc1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With