stringutils.cpp 960 B

12345678910111213141516171819202122232425262728
  1. #include "sys.hpp" // for libcwd
  2. #include "debug.hpp" // for libcwd
  3. #include "stringutils.h"
  4. /** transform a string into substrings by splitting at delimiter
  5. http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
  6. */
  7. void Tokenize(const string& str,
  8. vector<string>& tokens,
  9. const string& delimiters = " ")
  10. {
  11. // Skip delimiters at beginning.
  12. string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  13. // Find first "non-delimiter".
  14. string::size_type pos = str.find_first_of(delimiters, lastPos);
  15. while (string::npos != pos || string::npos != lastPos)
  16. {
  17. // Found a token, add it to the vector.
  18. tokens.push_back(str.substr(lastPos, pos - lastPos));
  19. // Skip delimiters. Note the "not_of"
  20. lastPos = str.find_first_not_of(delimiters, pos);
  21. // Find next "non-delimiter"
  22. pos = str.find_first_of(delimiters, lastPos);
  23. }
  24. }