How extract element from frame in c++11
Sep 28, 2018 at 1:13pm UTC
I try to write a code which one extract the element eg: "\ndata1:gdfste\ndata2:abcdfe\ndata3:wertqw"
affich data1 = gdfste
data2 = abcdfe
data3 = wertqw
Just now i write this line
Please Do you have some proposition
Thank's advance !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
string tmp = "\ndata1:gdfste\ndata2:abcdfe\ndata3:wertqw"
cout << tmp << endl;
auto p= tmp.find("data1" );
cout<< p<< endl;
if (string::npos !=p)
{
p+= tmp.size();
}
auto p1= tmp.find_first_of('\n' ,p);
if (string::npos!=p1)
{
cout<<"..." << tmp.substr(p,p1-p)<< endl;
}
return 0;
Sep 28, 2018 at 1:29pm UTC
A \n in stream is whitespace:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string foo( "\ndata1:gdfste\ndata2:abcdfe\ndata3:wertqw" );
std::istringstream bar( foo );
std::string line;
while ( bar >> line ) {
std::cout << "##" << line << "##\n" ;
}
}
Topic archived. No new replies allowed.