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