So I have string:
string message = "Hello\nHow are you?\nGood!"
I want to get it line by line in this format:
string line = "";
for (getline(message, line)) {
// do something with the line
}
How can I achieve this? It doesn’t matter if getline will be in use.
I tried to split the string by \n but I can’t go through it with for loop
>Solution :
getline requires an input stream to work on. Fortunately, we have std::stringstream which you can probably tell from the name is a stream of a string. Using that changes your code to
std::string line;
std::stringstream ss(message);
while(getline(ss, line)) {
// do something with the line
}