If I were considering this, I would see it and assume that what you are really trying to do is tokenize the string, and there are already good ways to do this.
The best way I've seen is boost::tokenizer . It allows you to specify how the string is divided, and then gives you a good iterator interface for repeating each value.
using namespace boost; string sample = "Hello,My,Name,Is,Doug"; escaped_list_seperator<char> sep("" , ",", "" ) tokenizer<escaped_list_seperator<char> > myTokens(sample, sep)
Output:
Hello My Name Is Doug
Edit If you don't need a boost dependency, you can also use getline with istringstream , as in this answer . To copy this answer a bit:
std::string str = "Hello,My,Name,Is,Doug"; std::istringstream stream(str); std::string tok1; while (stream) { std::getline(stream, tok1, ','); std::cout << tok1 << std::endl; }
Output:
Hello My Name Is Doug
It may not be what you ask, but I think it depends on your general problem that you are trying to solve.
source share