How can I split a string into string vector with \n?

Well I want to split a string into a vector string

1
2
3
4
5
6
7
  std::string frase = "My Name \n is something";
  std::vector<std::string>  lines;
std::string breakFrase;

.......?????????????....
lines.push_back(breakFrase);


I want to have:
lines.at(0) == "My Name "
lines.at(1) == "is something"

I tried somethings that I find online, but I`m always geting exactly:
lines.at(0) == "My Name \n is something"
I don't know if there's a simpler way... but I would just combine find() with substr():

1
2
3
4
5
6
7
8
9
10
std::string s = "whatever\nyou want\nto have";
std::vector<std::string> vec;

std::size_t pos;
while( (pos = s.find('\n')) != std::string::npos )
{
    vec.push_back( s.substr(0,pos) );
    s = s.substr(pos+1);
}
vec.push_back(s);
Last edited on
It worked, thanks =]
Last edited on
"/n" is not a newline character, it's a / character followed by an n character

"\n" is a newline character.


EDIT:

I just tried my approach:

http://ideone.com/RMx5MP

It works as I expected
Last edited on
Topic archived. No new replies allowed.