Read words from a string in C ++

I was wondering if there is a way to read all the “words” from a line of text.

the line will look like this: R, 4567890, Dwyer, Barb, CSCE 423, CSCE 486

Is there a way to use a comma as a separator to parse this string in an array, or something like that?

+3
source share
2 answers

Yes, use std::getlinestringstreams too.

std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";

std::istringstream iss(str);
std::vector<std::string> words;

while (std::getline(iss, str, ','))
  words.push_back(str);
+11
source
//#include sstream with angular braces in header files 

std::string str = "R,4567890,Dwyer,Barb,CSCE 423,CSCE 486";

std::istringstream iss(str,istringstream:in);

vector<std::string> words;

while (std::getline(iss, str, ','))
  words.push_back(str);
+1
source

Source: https://habr.com/ru/post/1718646/


All Articles