Hossam Salah
Hossam Salah

Reputation: 185

dividing a string into array after comma c++

I have the following string :-

CoursesExams = HUM001,Technical Writing,28/4/2016,HallA;CSE121,Computer Programming,3/5/2016,HallB]

  I want to split it after each ; into an array. How can I do that using c++?

Upvotes: 1

Views: 48

Answers (1)

marcinj
marcinj

Reputation: 49976

Use std::getline and stringstream:

std::string s = "HUM001,Technical Writing,28/4/2016,HallA;CSE121,Computer Programming,3/5/2016,HallB]";
std::vector<std::string> arr;
std::istringstream str(s);
std::string elem;

// getline reads str stream until comma is found, then returns string in elem
while(std::getline(str, elem, ',')) arr.push_back(elem);

for (auto& s : arr) std::cout << s << "\n";

Upvotes: 1

Related Questions