Split a string into tokens by a delimiter
| 38 | |
| 39 | // Split a string into tokens by a delimiter |
| 40 | std::vector<std::string> |
| 41 | SplitBy(const std::string& originalString, const std::string& delimiter, bool includeEmptyToken) |
| 42 | { |
| 43 | std::vector<std::string> tokens; |
| 44 | size_t cur = 0; |
| 45 | size_t next = 0; |
| 46 | while ((next = originalString.find(delimiter, cur)) != std::string::npos) |
| 47 | { |
| 48 | // Skip empty tokens, unless explicitly stated to include them. |
| 49 | if (next - cur > 0 || includeEmptyToken) |
| 50 | { |
| 51 | tokens.push_back(originalString.substr(cur, next - cur)); |
| 52 | } |
| 53 | cur = next + delimiter.size(); |
| 54 | } |
| 55 | // Get the remaining token |
| 56 | // Skip empty tokens, unless explicitly stated to include them. |
| 57 | if (originalString.size() - cur > 0 || includeEmptyToken) |
| 58 | { |
| 59 | tokens.push_back(originalString.substr(cur, originalString.size() - cur)); |
| 60 | } |
| 61 | return tokens; |
| 62 | } |
| 63 | |
| 64 | // Remove any preceding and trailing character specified in the characterSet. |
| 65 | std::string Strip(const std::string& originalString, const std::string& characterSet) |
no test coverage detected