How to tokenize a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string str = "the-text-in-the-string";
    std::istringstream iss(str);
    std::string token;
    
    while (std::getline(iss, token, '-')) {
        std::cout << token << std::endl;
    }
}


/*
run:

the
text
in
the
string

*/

 



answered Apr 10, 2025 by avibootz
...