CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Thu, 28 Aug 2025 05:45:13 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20080118115744
location: https://web.archive.org/web/20080118115744/https://www.daniweb.com/code/snippet797.html
server-timing: captures_list;dur=0.531988, exclusion.robots;dur=0.019448, exclusion.robots.policy;dur=0.009477, esindex;dur=0.010816, cdx.remote;dur=7.643469, LoadShardBlock;dur=197.766228, PetaboxLoader3.datanode;dur=154.263776
x-app-server: wwwb-app217
x-ts: 302
x-tr: 230
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
set-cookie: wb-p-SERVER=wwwb-app217; path=/
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
HTTP/2 200
server: nginx
date: Thu, 28 Aug 2025 05:45:13 GMT
content-type: text/html; charset=UTF-8
x-archive-orig-date: Fri, 18 Jan 2008 11:57:43 GMT
x-archive-orig-server: Apache/2.2
x-archive-orig-x-powered-by: PHP/5.1.6
x-archive-orig-set-cookie: bblastactivity=0; expires=Sat, 17-Jan-2009 11:57:43 GMT; path=/; domain=.daniweb.com
x-archive-orig-cache-control: private
x-archive-orig-pragma: private
x-archive-orig-connection: close
x-archive-guessed-content-type: text/html
x-archive-guessed-charset: utf-8
memento-datetime: Fri, 18 Jan 2008 11:57:44 GMT
link: ; rel="original", ; rel="timemap"; type="application/link-format", ; rel="timegate", ; rel="first memento"; datetime="Mon, 17 Dec 2007 22:27:04 GMT", ; rel="prev memento"; datetime="Mon, 17 Dec 2007 22:27:04 GMT", ; rel="memento"; datetime="Fri, 18 Jan 2008 11:57:44 GMT", ; rel="next memento"; datetime="Sat, 30 Aug 2008 09:54:03 GMT", ; rel="last memento"; datetime="Sun, 30 Aug 2009 07:22:09 GMT"
content-security-policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob: archive.org web.archive.org web-static.archive.org wayback-api.archive.org athena.archive.org analytics.archive.org pragma.archivelab.org wwwb-events.archive.org
x-archive-src: 51_1_20080118114004_crawl107-c/51_1_20080118114632_crawl104.arc.gz
server-timing: captures_list;dur=0.516625, exclusion.robots;dur=0.018626, exclusion.robots.policy;dur=0.008073, esindex;dur=0.010299, cdx.remote;dur=11.837741, LoadShardBlock;dur=149.351209, PetaboxLoader3.datanode;dur=148.235185, PetaboxLoader3.resolve;dur=85.754508, load_resource;dur=111.327896
x-app-server: wwwb-app217
x-ts: 200
x-tr: 362
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
content-encoding: gzip
string split inC++ using STL - c
string split inC++ using STL

What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 264,902 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 2,405 IT professionals currently interacting right now! If you are in the IT industry or are just a technology enthusiast, you might find just what you're looking for in DaniWeb. Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
32 Days Ago
Views: 646
Two methods for two distinct needs when splitting a string
Advertisements
c Syntax
#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(); }
Post Comment