Cavello wrote:
Hi, I'm curious as to how I can input a string of characters and then
output it in sections, seperating these sections according to a
particular value entered, for example a period. Say if I enter
'today.is.friday', I want the screen to output:
today
is
friday
How is this done?
This doesn't sound like homework, so I'll give you one I wrote. If it is
homework, then it's your loss not figuring it out on your own.
#include <vector>
#include <string>
// breaks apart a string into substrings separated by a character string
// does not use a strtok() style list of separator characters
// returns a vector of std::strings
std::vector<std::string> Explode (const std::string &inString,
const std::string &separator)
{
std::vector<std::string> returnVector;
std::string::size_type start = 0;
std::string::size_type end = 0;
while ((end=inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}
returnVector.push_back (inString.substr (start));
return returnVector;
}
Brian Rodenborn