i have string: system->ondrashek: nick aaasssddd není v žádné místnosti
, need aaasssddd
output string. output not same each time. must 2 whitespaces. tried substr or split, knowledge of c++ poor.
i find code:
#include <string> #include <iostream> int main() { const std::string str = "system->ondrashek: nick aaasssddd není v žádné místnosti"; size_t pos = str.find(" "); if (pos == std::string::npos) return -1; pos = str.find(" ", pos + 1); if (pos == std::string::npos) return -1; std::cout << str.substr(pos, std::string::npos); }
but not, need.
i assume want third word given string.
you have find second space, output sub-string second space end of string.
instead, need find third space, , output sub-string between 2 spaces.
so here modification.
#include <string> #include <iostream> int main() { const std::string str = "system->ondrashek: nick aaasssddd není v žádné místnosti"; size_t pos = str.find(" "); size_t start; size_t end; if (pos == std::string::npos) return -1; pos = str.find(" ", pos + 1); if (pos == std::string::npos) return -1; start = pos + 1; pos = str.find(" ", pos + 1); if (pos == std::string::npos) return -1; end = pos; std::cout << str.substr(start, end - start) << std::endl; }
Comments
Post a Comment