1std::stringstream test("this_is_a_test_string");
2std::string segment;
3std::vector<std::string> seglist;
4
5while(std::getline(test, segment, '_'))
6{
7 seglist.push_back(segment); //Spit string at '_' character
8}
1void tokenize(string &str, char delim, vector<string> &out)
2{
3 size_t start;
4 size_t end = 0;
5
6 while ((start = str.find_first_not_of(delim, end)) != string::npos)
7 {
8 end = str.find(delim, start);
9 out.push_back(str.substr(start, end - start));
10 }
11}
12
13int main()
14{
15 string s="a;b;c";
16 char d=';';
17 vector<string> a;
18 tokenize(s,d,a);
19 for(auto it:a) cout<<it<<" ";
20
21 return 0;
22}
1std::vector<std::string> split_string(const std::string& str,
2 const std::string& delimiter)
3{
4 std::vector<std::string> strings;
5
6 std::string::size_type pos = 0;
7 std::string::size_type prev = 0;
8 while ((pos = str.find(delimiter, prev)) != std::string::npos)
9 {
10 strings.push_back(str.substr(prev, pos - prev));
11 prev = pos + 1;
12 }
13
14 // To get the last substring (or only, if delimiter is not found)
15 strings.push_back(str.substr(prev));
16
17 return strings;
18}
19
1std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
2 std::stringstream ss(s);
3 std::string item;
4 while(std::getline(ss, item, delim)) {
5 elems.push_back(item);
6 }
7 return elems;
8}
9
1#include <boost/algorithm/string.hpp>
2
3std::string text = "Let me split this into words";
4std::vector<std::string> results;
5
6boost::split(results, text, [](char c){return c == ' ';});
1// splits a std::string into vector<string> at a delimiter
2vector<string> split(string x, char delim = ' ')
3{
4 x += delim; //includes a delimiter at the end so last word is also read
5 vector<string> splitted;
6 string temp = "";
7 for (int i = 0; i < x.length(); i++)
8 {
9 if (x[i] == delim)
10 {
11 splitted.push_back(temp); //store words in "splitted" vector
12 temp = "";
13 i++;
14 }
15 temp += x[i];
16 }
17 return splitted;
18}