Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:19:09+00:00 2026-05-31T00:19:09+00:00

I want to get some Strings out of a text file. I know how

  • 0

I want to get some Strings out of a text file. I know how to get the whole String of a text file with

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

}
while(!text.isNull());

That works fine to get all the text under the QString text, but I just need some specific strings out of the text,schematically like:

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

Can someone tell me how to do this or maybe even give me a small example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:19:10+00:00Added an answer on May 31, 2026 at 12:19 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get some useful information from NSError . If I print out
I want to get some text from a PHP page into an Android application.
So I want to read a text file but for some strange reason it
I have some text which is loaded from a resource file. Ordinarily, to get
I want to get some basic info of all contacts(I use api lvl 8).
I want to get some images from a movie and make a gif. What
I want to get some images from a flash webpage and I got the
Using Doctrine 2 I want to get some users that are contacts of another
i am developing and application in which i want to get some feeds from
I am using http://lite.facebook.com And i want to get some data from my account.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.