كيفية الحصول على سلسلة من ملف نصي مع مكتبات QT

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

  •  15-11-2019
  •  | 
  •  

سؤال

أريد الحصول على بعض الأوتار من ملف نصي.أنا أعرف كيفية الحصول على السلسلة بأكملها من ملف نصي مع giveacodicetagpre.

يعمل بشكل جيد للحصول على جميع النص بموجب نص QSTRING، لكنني فقط بحاجة إلى بعض الأوتار المحددة من النص، تخطيطي مثل: giveacodicetagpre.

هل يمكن لأحد أن يقول لي كيف أفعل هذا أو ربما حتى أعطني مثالا صغيرا؟

هل كانت مفيدة؟

المحلول

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