•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,979 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,818 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Two methods for two distinct needs when splitting a string
Last edited : Apr 18th, 2008.
#include <iostream> #include <string> #include <vector> #include <list> /*! \fn splitStr(std::list<std::string>& l, const std::string& seq, char s1, char s2, bool keeptok) \brief Splits a given std::string ( as param 'seq' ) into token separated by one starting character token and ending the token with a second given separator character. \param std::list<std::string>& reference to a string list that will receives all the resulting token \param std::string seq which is the string stream to split \param char s1 the first separator character that will start the token to be put into the resulting list \param char s2 the ending separator that will finish the token \param bool keeptok - optional boolean that is to be given TRUE if the separator characters are needed to be part of the tokens \return integer that has the number of token in the resulting list */ int splitStr(std::list<std::string>& l, const std::string& seq, char s1, char s2, bool keeptok) { typedef std::string::size_type ST; std::vector<int> tok_s1; std::vector<int> tok_s2; if(l.size()) l.clear(); ST pos=0, start=0, LEN=seq.size(); while( pos < LEN ){ if(seq[pos] == s1){ start = pos; if(s2){ while( (pos <LEN) && (seq[pos] != s2)) ++pos; if(pos <LEN){ tok_s2.push_back(pos); tok_s1.push_back(start); start = pos+1; } } else tok_s1.push_back(start); } ++pos; } if(s2){ if( ( tok_s1.size() != tok_s2.size() ) || (tok_s1.size() == 0) ){ //screwed: return the original string l.push_back(seq); return 1; } if(tok_s1.size()){ if(tok_s1[0]) l.push_back(seq.substr(0, tok_s1[0] - (keeptok ? 0: 1)) ); for(pos = 0; pos < tok_s1.size(); pos++){ if(pos>0){ int c = tok_s1[pos] - tok_s2[pos-1]; if(c > 1) l.push_back(seq.substr( tok_s2[pos-1]+1, c-1)); } l.push_back(seq.substr( tok_s1[pos], tok_s2[pos]-tok_s1[pos]+1 )); } } if( tok_s2.back() < (LEN-1)) l.push_back(seq.substr(tok_s2.back()+1, (LEN)-(tok_s2.back()+1))); } return l.size(); } /** \fn splitStr(std::list<std::string>& l, const std::string& seq, const std::string& _1cdelim, bool keeptoken=false, bool _removews=true ) \brief Splits a string into tokens separeted by supplied delimiters as a std::string. \param std::list<std::string>& L reference to the resulting string tokens \param std::string seq The string stream to split \param std::string _lcdelim - a std::string that contains all of the single delimiters \param bool keeptok -- same as the above function \param bool removews -- Set to TRUE if requiered to remove white space characters ( space, "\n\r" etc...) \return integer that has the number of token in the resulting list */ int splitStr(std::list<std::string>& L, const std::string& seq, const std::string& _1cdelim, bool keeptoken, bool _removews ) { typedef std::string::size_type ST; std::string delims = _1cdelim; std::string STR; if(delims.empty()) delims = "\n\r"; if(_removews) delims += " "; ST pos=0, LEN = seq.size(); while(pos < LEN ){ STR=""; // Init/clear the STR token buffer // remove any delimiters including optional (white)spaces while( (delims.find(seq[pos]) != std::string::npos) && (pos < LEN) ) ++pos; // leave if @eos if(pos==LEN) return L.size(); // Save token data while( (delims.find(seq[pos]) == std::string::npos) && (pos < LEN) ) STR += seq[pos++]; // put valid STR buffer into the supplied list //std::cout << "[" << STR << "]"; if( ! STR.empty() ) L.push_back(STR); } return L.size(); }
Comments (Newest First)
cscgal | The Queen of DaniWeb | Apr 18th, 2008
•
•
•
•
Moved.
vegaseat | Kickbutt Moderator | Mar 28th, 2008
•
•
•
•
This is really C++ syntax.
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)