Guest User

Untitled

a guest
Nov 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. template< typename char_type>
  2. static inline std::vector< std::basic_string< char_type > > basic_split(std::basic_string<char_type > string, const std::basic_string<char_type >& delim, bool keepEmpty = false) {
  3.     std::vector < std::basic_string< char_type > > tokens;
  4.     typename std::basic_string< char_type >::size_type pos;
  5.     while (( pos = string.find(delim)) != string.npos) {
  6.         if (pos) {
  7.             std::basic_string<char_type> subString = string.substr(0, pos);
  8.             if (subString.length() > 0 || keepEmpty)
  9.                 tokens.push_back(subString);
  10.         } else if (keepEmpty) {
  11.             tokens.push_back(std::basic_string<char_type >());
  12.         }
  13.         string.erase(0, pos + delim.length());
  14.     }
  15.     if (string.length()) {
  16.         tokens.push_back(string);
  17.     }
  18.     return tokens;
  19. }
  20.  
  21. static inline std::vector< std::string > split(std::string string, const std::string& delim, bool keepEmpty = false){
  22.     return basic_split<char>(string, delim, keepEmpty);
  23. }
  24.  
  25. static inline std::vector< std::wstring > split(std::wstring string, const std::wstring& delim, bool keepEmpty = false){
  26.     return basic_split<wchar_t>(string, delim, keepEmpty);
  27. }
Add Comment
Please, Sign In to add comment