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