c++ - Qt-拆分字符串但将分隔符保留在数组中

标签 c++ qt

我正在尝试使用 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/

相关文章:

python - 如何在 PyQt/PySide 中表征 createIndex 的 "pointer"参数?

c++ - QWebView 不能在真正的打印机上打印多页?

c++ - 对象层次结构的 "Implementation"- "the easiest way"或如何避免虚拟继承?

c++ - 将巨大的二维 vector 写入文本文件太慢,如何提高性能?

c++ - 如何将从Ruby数组中取出的字符串转换为C/C++字符串

c++ - 发送固定长度的 header

c++ - 菜单栏仅在使用 Qt5 在 MacOS 上切换应用程序/桌面后显示

c++ - 在 Windows 上使用 Qt5 进行奇怪的延迟绘画

java - 开源 Linux 服务器项目

c++ - 衰减的 lambda 指向的函数存储在哪里?它是如何释放的?