Read different QStrings with the same previous and the same following word? QString::indexOf

StackOverflow https://stackoverflow.com//questions/9626952

  •  09-12-2019
  •  | 
  •  

Question

I read two QStrings (ValueOne,ValueTwo) out of a file with (just a basic example)

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()));
    }
}

As you see the source code can't difference between ValueOne and ValueTwo just by "start" and "end", because both QString::mid() methods (which proceed line by line as far as I know) have the same start position and the same length (see http://qt-project.org/doc/qt-4.8/qstring.html#mid ). Therefore I thought if the whole string was one line like

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

I could difference between the two values with QString s = "yes start" and QString s2 = "no start". So would convert the multiline String into a one line String be a solution and how can I do this? or Is there another maybe better solution? greetings

Was it helpful?

Solution

As I already mention in your other question I would prefer QRegExp. It seems to be more readable.

If your first string is the 2n value always and your second string is 2n+1 you could use the modulo operator:

#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;
}

OTHER TIPS

With something similar to the code below you could find all the strings between "start" and "end". Put the search for "start" and "end" in a loop and use the offset parameter of indexOf to continue searching for new delimiters after the first one.

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()));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top