I have a QT Project where I get the full name as a QString variable fullName
from user input. I'd like to store the first name ( firstName
) and last name ( surname
) in their own QString variables by extracting them from fullName
.
The user input could end up adding their middle names as well so I need to to work for all kind of examples like that, eg. user input: Barry Michael Doyle
would have to give firstName
the value of Barry
and surname
the value of Doyle
.
I'm not very good at QString Manipulation as I've only recently started using the Qt Library. I'd appreciate all the help I could get.
Qt's documentation on QString is pretty thorough and has a lot of examples on how to use it. You can find it here: http://doc.qt.io/qt-5/qstring.html
In general, parsing an input string that could have mistakes in it is difficult (what would the expected behavior of an input string of "BarryDoyle" be? What about "Barrydoy le"?)
For the simple case of splitting a string using the space character you can take a look at QString::split ( http://doc.qt.io/qt-5/qstring.html#split )
QString str = "Barry Michael Boyle"
QStringList list_str = str.split(" ");
QString first = list_str.first();
QString last = list_str.back();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.