0

我从一个文件中读取了两个 QStrings (ValueOne,ValueTwo) (只是一个基本示例)

int main(int argc, char *argv[]) {
    QString x = ("yes " 
                 "start ValueOne end" 
                 "no" 
                 "start ValueTwo end");

   //try to read ValueOne
    QString s = "start";
    QString e = "end";
    int start = x.indexOf(s, 0, Qt::CaseInsensitive); 
    int end = x.indexOf(e, Qt::CaseInsensitive); 

    if(start != -1){ 

        QString y = x.mid(start + s.length(), (end - (start + s.length()))); 

        qDebug() << y << (start + s.length()) << (end - (start + s.length()));

    //try to read ValueTwo
    QString s2 = "start";
    QString e2 = "end";
    int start2 = x.indexOf(s2, 0, Qt::CaseInsensitive); 
    int end2 = x.indexOf(e2, Qt::CaseInsensitive); 

    if(start2 != -1){ 

        QString y2 = x.mid(start2 + s.length2(), (end2 - (start2 + s.length2()))); 

        qDebug() << y2 << (start2 + s.length2()) << (end2 - (start2 + s.length2()));
    }
}

如您所见,ValueOne 和 ValueTwo 仅通过“start”和“end”无法区分,因为两个 QString::mid() 方法(据我所知逐行进行)具有相同的起始位置和相同的长度(参见http://qt-project.org/doc/qt-4.8/qstring.html#mid)。因此我想如果整个字符串是一行

QString x = "yes start ValueOne end no start ValueTwo end ";

我可以用 QString s = "yes start" 和 QString s2 = "no start" 来区分这两个值。那么将多行字符串转换为单行字符串是一种解决方案,我该怎么做?或者还有其他更好的解决方案吗?问候

4

2 回答 2

2

正如我在您的其他问题中已经提到的那样,我更喜欢QRegExp。它似乎更具可读性。

如果您的第一个字符串2n始终是值,而您的第二个字符串是2n+1您可以使用模运算符:

#include <QDebug>
#include <QString>
#include <QStringList>
#include <QRegExp>

int main()
{
    QString x = ("yes \nstart ValueOne end \nno \nstart ValueTwo end\n"
                "yes \nstart ValueThree end \nno \nstart ValueFour end ");

    QStringList y1;
    QStringList y2;

    // create regular expression
    QRegExp rx("start\\s+(.+)\\s+end\\s+", Qt::CaseInsensitive);

    // don't try to get the largest match (start ValueOne ... ValueFour end)
    // minimal match should be (start ValueOne end)
    rx.setMinimal(true);

    int pos=0;
    int i=0; // counter

    // look for possible matches
    QString match;
    while ((pos=rx.indexIn(x, pos)) != -1) {
        i+=1; // increase counter for every match
        match=rx.cap(1); // get first match in (.+)

        // use modulo to distinguish between y1/y2    
        if (i % 2) {
            y1 << match;
        } else {
            y2 << match;
        }

        pos+=rx.matchedLength();
    }

    qDebug() << "y1:" << y1;
    qDebug() << "y2:" << y2;

    return 0;
}
于 2012-03-09T07:11:20.820 回答
1

使用类似于以下代码的内容,您可以找到“start”和“end”之间的所有字符串。将搜索“start”和“end”放在一个循环中,并使用 indexOf 的 offset 参数在第一个分隔符之后继续搜索新的分隔符。

int main(int argc, char *argv[]) {
    QString x = ("yes /nstart ValueOne end /nno /nstart ValueTwo end ");

    QString s = "start";
    QString e = "end";

    // Look for all the strings between "start" and "end"    
    for(int offset(0); offset < x.length(); )
    {
        // Search for "start" starts from offset
        int start = x.indexOf(s, offset, Qt::CaseInsensitive); 

        if(start < 0){
            break;
        }

        // Search for "end" starts from the position of "start"
        int end = x.indexOf(e, start, Qt::CaseInsensitive); 
        if(end < 0){
            break;
        }

        // Next search for "start" will start from the current position of "end"
        offset = end;

        QString y = x.mid(start + s.length(), (end - (start + s.length()))); 
        qDebug() << y << (start + s.length()) << (end - (start + s.length()));
    }
}
于 2012-03-09T00:28:46.937 回答