Timeline for How do I tokenize a string in C++?
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 16, 2022 at 18:08 | comment | added | guitarpicva | @Parham, sadly this also seems to skip empty fields, such as: ` a,b,c,,,f,g` Which returns a b c f g as the 5 members of the vector instead of including the empty strings. Indexed content suffers. :( It is pretty common in things such as NMEA GPS sentences to have multiple empty fields in it's indexed, character separated string data. | |
Aug 10, 2021 at 5:57 | comment | added | HMartyrossian | Replaced "DELIMITER" with "delimiters": std::vector<string> tokenize(const std::string& str, const string& delimiters) { std::vector<string> result; size_t start = str.find_first_not_of(delimiters), end = start; while (start != std::string::npos) { // Find next occurrence of delimiter end = str.find(delimiters, start); // Push back the token found into vector result.push_back(str.substr(start, end - start)); // Skip all occurrences of the delimiter to find new start start = str.find_first_not_of(delimiters, end); } return result; } | |
Aug 10, 2021 at 5:50 | comment | added | HMartyrossian | I voted "Up" for Parham's solution and modified it a bit: std::vector<string> tokenize(const std::string& str, const string& delimiters) { std::vector<string> result; size_t start = str.find_first_not_of(DELIMITER), end = start; while (start != std::string::npos) { // Find next occurrence of delimiter end = str.find(DELIMITER, start); // Push back the token found into vector result.push_back(str.substr(start, end - start)); // Skip all occurrences of the delimiter to find new start start = str.find_first_not_of(DELIMITER, end); } return result; } | |
Oct 5, 2018 at 12:38 | comment | added | Beginner | @user755921 multiple delimiters are skipped when finding the start position with find_first_not_of. | |
Nov 27, 2015 at 19:28 | comment | added | user755921 | This is a good one but I think you need to use find_first_of() instead of find() for this to work properly with multiple delimiters. | |
Mar 1, 2015 at 22:18 | history | edited | Parham | CC BY-SA 3.0 |
added 50 characters in body
|
Mar 1, 2015 at 22:07 | history | edited | Parham | CC BY-SA 3.0 |
simpler while boolean expr, updated demo link
|
Mar 1, 2015 at 21:32 | history | edited | Parham | CC BY-SA 3.0 |
added 9 characters in body
|
Feb 28, 2015 at 23:18 | history | answered | Parham | CC BY-SA 3.0 |