Qt QString maxsplit argument

情到浓时终转凉″ 提交于 2019-12-11 08:26:14

问题


Python strings have a function split() that can take a maxsplit argument (from Python docs):

If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

Can QStrings do this? I looked at the docs and there's no split() overload that takes an integer equivalent to maxsplit as argument.


回答1:


It doesn't seem like it, though it seems to be trivial to implement -

QString str("How are all of you doing");
QStringList list = str.split(' ').mid(0, maxSplit);
QString remainingStr = str.section(' ', maxSplit);
list << remainingStr;

Or if you want to be more performant, you can just copy the code from QString::split, and add the extra feature. The code is there at qtbase/src/corelib/tools/qstring.cpp. You just need to add && list.size() <= maxSplit in the while loop.



来源:https://stackoverflow.com/questions/18343253/qt-qstring-maxsplit-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!