我正在尝试使用 delimiters
拆分 string
但我想将 delimiters
保留在数组中。代码:
QRegExp rx("(\\+|\\-|\\*|\\/)");
QStringList query = text.split(rx);
输入:
2+3
这会给我一个数组 2,3 但我想要 2,+,3
有什么办法吗?
最佳答案
您可以针对您的问题找到解决方案。 试试这个代码:
#include <iostream>
#include <QStringList>
#include <QRegExp>
int main()
{
QString text = "2+3-3-4/5+9+0"; // Input, you can write you own code to take input
QRegExp rx("(\\+|\\-|\\*|\\/)");
QStringList query = text.split(rx);
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(text, pos)) != -1) {
++count;
pos += rx.matchedLength();
query.insert(count * 2-1, QString(text[pos - 1]));
}
return 0;
}
关于c++ - Qt-拆分字符串但将分隔符保留在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45765531/