How do you extract a string that is in between a char/string in C++? -


let's have simple string:

string example = "phone number: xxxxxxx," 

where x random values given me, different. how extract x's? "phone number: " doesn't change.

what have (using thomas matthews technique below)

const char search_text[] = "phone number: "; std::string::size_type start_posn = example.find(search_text); std::string::size_type end_posn = example.find(",", start_posn); const unsigned int length = end_posn - start_posn - sizeof(search_text) - 1; cout << "length: " << length << endl; std::string data = example.substr(start_posn, length); cout << data << endl; 

what if had string example = "phone number: xxxxxxx, date: xxxxxxxx,"?

hmmm, looks search "phone number: " , determine index or position after phrase. requirements infer there "," after data want.

so, want substring between ": " , before ",". in olden days, search "," , position. number of characters extract obtained subtracting 2 indices:
edit 1

const char search_text[] = "phone number: "; std::string::size_type start_posn = example.find(search_text); if (start_posn != std::string::npos) {    start_posn += sizeof(search_text) - 1; } std::string::size_type end_posn   = example.find(",", start_posn); const unsigned int length = end_posn - start_posn; std::string data = example.substr(start_posn, length); 

note: above code not handle error cases find method returns std::string::npos.

using above technique, how extract data after "date: "?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -