#include<iostream>
#include<sstream>
#include<string>
int main ()
{
std::string str = "abc,def,123,ghi,jkl,456,mno";
std::string strings[100];
std::stringstream to_split(str);
std::string temp;
int i = 0;
while( getline(to_split, temp, ',') )
strings[i++] = temp;
for(int x = 0; x < i; ++x)
std::cout << strings[x] << std::endl;
}
Stringstreams was created exactly for these situations. you can take c-string with str.c_str(), then split it with strtok() function from <cstring> http://cplusplus.com/reference/cstring/strtok/
You can use find() and substr() by putting it inside loop like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include<string>
int main ()
{
std::string str = "abc,def,123,ghi,jkl,456,mno";
std::string strings[100];
unsigned string_num = 0;
unsigned position;
do {
position = str.find(",");
strings[string_num++] = str.substr(0, position);
str = str.substr(position + 1);
} while(position != str.npos);
for(int x = 0; x < string_num; ++x)
std::cout << strings[x] << std::endl;
}
But is s;ow and is reinventing a wheel. Square wheel, I say.