Как получить строку из текстового файла с библиотеками QT

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

  •  15-11-2019
  •  | 
  •  

Вопрос

Я хочу получить некоторые строки из текстового файла.Я знаю, как получить всю строку текстового файла с

QTextStream Stream (GEO); 
QString text;    
 do    
{    
text = Stream.readLine();  

}
while(!text.isNull());
.

Это нормально работает, чтобы получить весь текст под текстом QString, но мне просто нужны некоторые конкретные строки из текста, схематически как:

if the text "start" appears in the Qstring text (or the QTextStream Stream) 

save the following text under QString First

until the text "end" appears
.

Может кто-нибудь сказать мне, как это сделать или, может быть, даже дать мне небольшой пример?

Это было полезно?

Решение

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!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top