0

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.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • 2
    i think you should first well define what your possible inputs are, e.g., how do the names look? (all possibilities). and then "define" what you call first name there and what last name? – Giorgi Moniava Feb 27 '15 at 21:36
  • My possible inputs are "firstName lastName". Thought I made that clear in my question. – Barry Michael Doyle Feb 27 '15 at 22:46

1 Answers1

2

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();
Prismatic
  • 3,338
  • 5
  • 36
  • 59
  • It's all for a task that I'm doing where we have to expect that the user doesn't make errors in their input so I'm not too worried about that... But It must work for multiple middle names where I just get last name and first name (provided last night is only one word) – Barry Michael Doyle Feb 27 '15 at 21:52