简体   繁体   English

从全名QString中获取名字和姓氏

[英]Get first name and last name out of a full name QString

I have a QT Project where I get the full name as a QString variable fullName from user input. 我有一个QT项目,我从用户输入中获得一个全名作为QString变量fullName I'd like to store the first name ( firstName ) and last name ( surname ) in their own QString variables by extracting them from fullName . 我想通过从fullName提取它们来将名字( firstName )和姓氏( surname )存储在它们自己的QString变量中。

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 . 用户输入: Barry Michael Doyle将不得不给firstName价值Barrysurname的值Doyle

I'm not very good at QString Manipulation as I've only recently started using the Qt Library. 我不太擅长QString操作,因为我最近才开始使用Qt库。 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. Qt上有关QString的文档非常详尽,并提供了许多有关如何使用它的示例。 You can find it here: http://doc.qt.io/qt-5/qstring.html 您可以在这里找到它: 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"?) 通常,很难解析可能有错误的输入字符串(“ BarryDoyle”输入字符串的预期行为是什么?“ 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 :: 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM