Results 1 to 6 of 6

Thread: index of a string that appears n times

  1. #1
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Post index of a string that appears n times

    hello i am able to get index of "@" in c++ but not in qt. i dont know what i have to use to find("@",found+1,q)

    Qt Code:
    1. #include <iostream> // std::cout
    2. #include <string> // std::string
    3.  
    4. int main ()
    5. {
    6. std::string str ("msel@-txt/l<seq_name>@#ifndef I_MSEL_@-txt/u<seq_name>@_H");
    7. std::string tag ("@");
    8.  
    9.  
    10.  
    11. // different member versions of find in the same order as above:
    12. std::size_t found = str.find(tag);
    13. if (found!=std::string::npos)
    14. std::cout << "first '@' found at: " << found << '\n';
    15.  
    16. for (int i = 0; i <= 5; i++)
    17. { int q =1;
    18. found=str.find("@",found+1,q);
    19. q++;
    20. if (found!=std::string::npos)
    21. std::cout << " '@' found at: " << found << '\n';
    22. }
    23.  
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: index of a string that appears n times

    Hello,

    From Qt Creator help - QString
    http://doc.qt.io/qt-5/qstring.html#indexOf-6

    #include <QString>

    QString x = "sticky question";
    QString y = "sti";
    x.indexOf(y); // returns 0
    x.indexOf(y, 1); // returns 10
    x.indexOf(y, 10); // returns 10
    x.indexOf(y, 11); // returns -1, if not found

    Regards

  3. The following user says thank you to jimbo for this useful post:

    deepikha (19th October 2017)

  4. #3
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: index of a string that appears n times

    hi i have one more doubt string that indicates with character '@' and next to it string in the "" data should be extracted and stored in variable so for example
    string str ("msel@"txt/l<seq_name>"#ifndef I_MSEL_@"txt/u<seq_name>"_H");

    i need output to be -txt/l<seq_name>
    then -txt/u<seq_name>

    can you help me out with this
    Last edited by deepikha; 19th October 2017 at 12:17.

  5. #4
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: index of a string that appears n times

    You now have the index of @.

    From the link I gave:-
    look for
    QString->length
    QString->right
    QString->left

    Just remember the index starts at zero.

  6. The following user says thank you to jimbo for this useful post:

    deepikha (20th October 2017)

  7. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: index of a string that appears n times

    Looks like you want to split the string, see if this helps
    Qt Code:
    1. #include <QString>
    2. #include <QDebug>
    3.  
    4. int main ()
    5. {
    6. QString str ("msel@-txt/l<seq_name>@#ifndef I_MSEL_@-txt/u<seq_name>@_H");
    7. QString tag ("@");
    8.  
    9. QStringList list = str.split(tag);
    10.  
    11. foreach(const QString & s, list)
    12. qDebug() << s;
    13.  
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. The following user says thank you to Santosh Reddy for this useful post:

    deepikha (20th October 2017)

  9. #6
    Join Date
    Oct 2017
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: index of a string that appears n times

    Well for c++ - native : you can use iterators..


    void findInNormal()
    {
    std::string str ("msel@-txt/l<seq_name>@#ifndef I_MSEL_@-txt/u<seq_name>@_H");
    auto found = std::find(str.begin(),str.end(),'@');

    while(found != str.end())
    {
    qDebug() << "'@' found at: " << std::distance(str.begin(),found++) << '\n';
    found = std::find(found,str.end(),'@');
    }
    }
    in qt - string style might be like this...


    void findInQt()
    {
    std::string str ("msel@-txt/l<seq_name>@#ifndef I_MSEL_@-txt/u<seq_name>@_H");
    QString qstr = QString::fromStdString(str);
    auto index = qstr.indexOf('@');
    while(index != -1)
    {
    qDebug() << "'@' found at: " << index << '\n';
    index = qstr.indexOf('@',++index);
    }
    }

  10. The following user says thank you to geekowl for this useful post:

    deepikha (20th October 2017)

Similar Threads

  1. index of a string that appears n times
    By deepikha in forum General Discussion
    Replies: 1
    Last Post: 22nd October 2017, 22:33
  2. Nested loop only loops 3 times instead of 710 times
    By ayanda83 in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2016, 18:12
  3. Replies: 2
    Last Post: 4th February 2014, 11:47
  4. PyQT Listview index to string
    By Dariusz in forum Newbie
    Replies: 1
    Last Post: 28th September 2013, 09:05
  5.  appears in QTextBrowser
    By szisziszilvi in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2011, 08:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.