Frage

Ich möchte einige Saiten aus einer Textdatei herausholen.Ich weiß, wie Sie die gesamte Zeichenfolge einer Textdatei mit erhalten generasacodicetagpre.

das funktioniert gut, um den gesamten Text unter dem QString-Text zu erhalten, aber ich brauche nur einige spezifische Saiten aus dem Text, schematisch wie: generasacodicetagpre.

Kann mir jemand sagen, wie man das tun soll oder vielleicht sogar ein kleines Beispiel geben kann?

War es hilfreich?

Lösung

One thing you can use is to get the indexes of the "start" and "end" with indexOf() and the just use:

QString x = "start some text here end";
QString s = "start";
QString e = "end"
int start = x.indexOf(s, 0, Qt::CaseInsensitive);  // returns the first encounter of the string 
int end = x.indexOf(e, Qt::CaseInsensitive);  // returns 21

if(start != -1) // we found it
    QString y = x.mid(start + s.length(), end);

or midRef if you dont want to create a new list. You might have to handle "end" aswell, otherwise you might go from 0 to -1 which wouldnt return anything. Maybe (end > start ? end : start)

Edit: Nevermind. If end == -1 that just means that it will return everything until the end (per default the second parameter is -1). If you don't want this you can go with my example instead and use some kind of if-statement when choosing the "end"

Edit: Noticed that I missread the doc and this will def. work:

#include <QDebug>

int main(int argc, char *argv[]) {
    QString x = "start some text here end";
    QString s = "start";
    QString e = "end";
    int start = x.indexOf(s, 0, Qt::CaseInsensitive); 
    int end = x.indexOf(e, Qt::CaseInsensitive); 

    if(start != -1){ // we found it
        QString y = x.mid(start + s.length(), ((end - (start + s.length())) > -1 ? (end - (start + s.length())) : -1)); // if you dont wanna pass in a number less than -1
        or
        QString y = x.mid(start + s.length(), (end - (start + s.length()))); // should not be any issues passing in a number less than -1, still works

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

This produces the following resoults. The last two numbers are where "start" ends and "end" begins.

x = "start some text here end" => " some text here " 5 16

x = " some text here end" => no outprint

x = "testing start start some text here end" => " start some text here " 13 22

x = "testing start start some text here" => " start some text here" 13 -14

Or you can do it by using regEx. Wrote a very simple snippet here for you:

#include <QDebug>
#include <QRegExp>

int main(int argc, char *argv[]) {
    QRegExp rxlen("(start)(.*(?=$|end))");
    rxlen.setMinimal(true); // it's lazy which means that if it finds "end" it stops and not trying to find "$" which is the end of the string 
    int pos = rxlen.indexIn("test start testing some text start here fdsfdsfdsend test ");

    if (pos > -1) { // if the string matched, which means that "start" will be in it, followed by a string 
        qDebug() <<  rxlen.cap(2); // " testing some text start here fdsfdsfds" 
    }
}

This works even if you done have "end" in the end, then it just parse to the end of the line. Enjoy!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top