User Name Password Register
DaniWeb IT Discussion Community
All
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:
Dec 16th, 2007
Views: 4,642
Two methods for two distinct needs when splitting a string
Last edited : Apr 18th, 2008.
cplusplus Syntax
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <list>
  5.  
  6. /*!
  7.   \fn splitStr(std::list<std::string>& l, const std::string& seq, char s1, char s2, bool keeptok)
  8.   \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.
  9.   \param std::list<std::string>& reference to a string list that will receives all the resulting token
  10.   \param std::string seq which is the string stream to split
  11.   \param char s1 the first separator character that will start the token to be put into the resulting list
  12.   \param char s2 the ending separator that will finish the token
  13.   \param bool keeptok - optional boolean that is to be given TRUE if the separator characters are needed to be part of the tokens
  14.   \return integer that has the number of token in the resulting list
  15. */
  16. int splitStr(std::list<std::string>& l, const std::string& seq, char s1, char s2, bool keeptok)
  17. {
  18. typedef std::string::size_type ST;
  19. std::vector<int> tok_s1;
  20. std::vector<int> tok_s2;
  21. if(l.size()) l.clear();
  22. ST pos=0, start=0, LEN=seq.size();
  23. while( pos < LEN ){
  24. if(seq[pos] == s1){
  25. start = pos;
  26. if(s2){
  27. while( (pos <LEN) && (seq[pos] != s2)) ++pos;
  28. if(pos <LEN){
  29. tok_s2.push_back(pos);
  30. tok_s1.push_back(start);
  31. start = pos+1;
  32. }
  33. }
  34. else tok_s1.push_back(start);
  35. }
  36. ++pos;
  37. }
  38. if(s2){
  39. if( ( tok_s1.size() != tok_s2.size() ) || (tok_s1.size() == 0) ){
  40. //screwed: return the original string
  41. l.push_back(seq);
  42. return 1;
  43. }
  44. if(tok_s1.size()){
  45. if(tok_s1[0]) l.push_back(seq.substr(0, tok_s1[0] - (keeptok ? 0: 1)) );
  46. for(pos = 0; pos < tok_s1.size(); pos++){
  47. if(pos>0){
  48. int c = tok_s1[pos] - tok_s2[pos-1];
  49. if(c > 1) l.push_back(seq.substr( tok_s2[pos-1]+1, c-1));
  50. }
  51. l.push_back(seq.substr( tok_s1[pos], tok_s2[pos]-tok_s1[pos]+1 ));
  52. }
  53. }
  54. if( tok_s2.back() < (LEN-1)) l.push_back(seq.substr(tok_s2.back()+1, (LEN)-(tok_s2.back()+1)));
  55. }
  56. return l.size();
  57. }
  58. /**
  59.   \fn splitStr(std::list<std::string>& l, const std::string& seq, const std::string& _1cdelim, bool keeptoken=false, bool _removews=true )
  60.   \brief Splits a string into tokens separeted by supplied delimiters as a std::string.
  61.   \param std::list<std::string>& L reference to the resulting string tokens
  62.   \param std::string seq The string stream to split
  63.   \param std::string _lcdelim - a std::string that contains all of the single delimiters
  64.   \param bool keeptok -- same as the above function
  65.   \param bool removews -- Set to TRUE if requiered to remove white space characters ( space, "\n\r" etc...)
  66.   \return integer that has the number of token in the resulting list
  67. */
  68. int splitStr(std::list<std::string>& L, const std::string& seq, const std::string& _1cdelim, bool keeptoken, bool _removews )
  69. {
  70. typedef std::string::size_type ST;
  71. std::string delims = _1cdelim;
  72. std::string STR;
  73. if(delims.empty()) delims = "\n\r";
  74. if(_removews) delims += " ";
  75.  
  76. ST pos=0, LEN = seq.size();
  77. while(pos < LEN ){
  78. STR=""; // Init/clear the STR token buffer
  79. // remove any delimiters including optional (white)spaces
  80. while( (delims.find(seq[pos]) != std::string::npos) && (pos < LEN) ) ++pos;
  81. // leave if @eos
  82. if(pos==LEN) return L.size();
  83. // Save token data
  84. while( (delims.find(seq[pos]) == std::string::npos) && (pos < LEN) ) STR += seq[pos++];
  85. // put valid STR buffer into the supplied list
  86. //std::cout << "[" << STR << "]";
  87. if( ! STR.empty() ) L.push_back(STR);
  88. }
  89. return L.size();
  90. }
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

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 12:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC