### date and time separeta from one string

Hello i need separate this.
 `5.5.10:30`

into two string
string zinter1 = "5.5."
string zinter2 = "10:30"

Can anybody help me ? i trying something with getline but i never separate it right.

 ``123456789101112131415161718192021222324`` `````` string intervalz = "10.5.10:30"; string zinter1, zinter2; int rozdel = 0; istringstream isss(intervalz); while (getline(isss, intervalz, ',')) { if(rozdel == 0) { rozdel++; zinter1 = intervalz; } else if(rozdel == 1) { zinter1 = zinter1 + intervalz; rozdel++; } else if (rozdel == 2) { zinter2 = intervalz; } else endfail(); } cout << zinter1 << "***" << zinter2;``````
Last edited on
Hmmm, this all looks very confusing...

Try something like this:
 ``1234567891011121314151617181920212223`` `````` string intervalz = "10.5.10:30\n"; string zinter1, zinter2; int rozdel = 0; //istringstream isss(intervalz); Why are you doing this? /* * I'm assuming that the istringstream is so you can * easily adapt this later to take user input... you don't * need to do that. Just don't overwrite the string. */ //while (getline(cin, intervalz)) //{ size_t stringIndex = 0; /* * You could put the following line into a for loop to * find the n'th '.', but you want the second so doing * it twice is easier. */ stringIndex= intervalz.find_first_of('.', stringIndex); stringIndex= intervalz.find_first_of('.', stringIndex); zinter1= intervalz.substr(0, stringIndex); // from the start to the second '.' character zinter2= intervalz.substr(stringIndex, string::npos); // from the second '.' character to the end cout << zinter1 << "***" << zinter2; //} ``````

I haven't test the code so it may need debugging, you seem to be doing some strange things with stringstreams... I'm not sure why, so this may not be an appropriate solution.

Dax.
Last edited on
 ``12345678`` `````` string intervalz = "5.5.10:30"; string zinter1, zinter2; size_t pos = intervalz.rfind('.'); if (pos != string::npos) { zinter1 = intervalz.substr(0, pos+1); zinter2 = intervalz.substr(pos+1); }``````
Topic archived. No new replies allowed.