동일한 이전과 같은 단어로 다른 qstring을 읽으십시오.QString :: indexof

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

  •  09-12-2019
  •  | 
  •  

문제

i (기본 예제) 파일에서 두 개의 QStrings (Valesone, 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()));
    }
}
.

소스 코드가 "시작"및 "끝"만으로 Valesone과 Valuetwo간에 차이가 없을 수 없으므로 QString :: mid (in mid) 메소드 (알고있는 한 줄로 줄을 진행됨)가 있으므로동일한 시작 위치 및 동일한 길이 ( http : // qt-project 참조).org / doc / qt-4.8 / qstring.html # 중순 ).그러므로 전체 문자열이 와 같은 한 줄이되었는지 생각했습니다.

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

I 값이 QSTRING S="YES START"와 QSTRING S2="시작되지 않음"과 차이가있을 수 있습니다.따라서 여러 줄 문자열을 한 줄 문자열로 변환하면 해결책이고 어떻게이 작업을 수행 할 수 있습니까?아니면 더 나은 해결책이 더 나은 것입니까? 인사말

도움이 되었습니까?

해결책

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

다른 팁

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()));
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top