Trying to split a string out into parts. The format will always be like this name from number on date
need to split out name, number, and date
code:
string name, number, date;
size_t from = s.find(" from ");
size_t on = s.find(" on ");
name = s.substr(0, from);
number = s.substr(from + 6, on);
date = s.substr(on + 4);
cout << "name:" << name << ", num:" << number << ", date:" << date << endl;
sample input: John Smith from N656 on 01012013
sampe output: name:John Smith, num: N656 on 01012013, date:01012013
So there’s obviously a problem when trying to create the substring number but I’m very curious what could be causing this since the other two are working.
I think you’re assuming that
s.substraccepts start/end index.This is what it actually is:
s.substr(pos, size)So, in an ugly way: