Results 1 to 3 of 3

Thread: QStrung::replace problem

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QStrung::replace problem

    Hi,
    I am trying to replace some parts of a string, and for some reason, it works very strange so I was hoping someone here could point out what it is I am doing wrong...
    I am reading lines out of a text file, and in each line I am sarching for ';' and replacing them with "###".
    But I don't want to replace all ';'s just some that apear in certain places.
    Here is the code:
    Qt Code:
    1. //text contains the current read line
    2. //count 9 ';' and replace the 10th with "### "
    3. for(int i=0; i<10 && pos!=-1; ++i)
    4. {
    5. if(pos !=-1)
    6. {
    7. pos++;
    8. pos = text.indexOf(";",pos);
    9. }
    10. }
    11. qDebug()<<"pos:"<<pos;
    12. if(pos !=-1)
    13. text.replace(pos,"###")
    14. qDebug()<<text;
    To copy to clipboard, switch view to plain text mode 

    The debug output is half correct, which is the strange part.
    The pos value is correct, that is, the position in the string is the correct position of the ';' that should be replaced, but the second debug, which returns the string after the change is with out any change in the string.
    The next strings are being changed, but with no corelation to where they should...

    Any help is very appreciated.
    Thanks.

    EDIT:
    I found my error - I forgot a parameter the replace method.
    Last edited by high_flyer; 7th September 2006 at 11:55.

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStrung::replace problem

    Quote Originally Posted by high_flyer
    Qt Code:
    1. for (int i=0; i<10 && pos!=-1; ++i)
    2. {
    3. if (pos !=-1)
    4. {
    5. pos++;
    6. pos = text.indexOf(";",pos);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    This if statement is unnecessary. If you entered the loop body, than pos != -1 is always true, because it's in loop condiiton.

    Quote Originally Posted by high_flyer
    EDIT:
    I found my error - I forgot a parameter the replace method.
    Yes, I noticed. There is no replace() method with only 2 parameters according to QT documentation. Didn't compiler give you any errors or warnings?
    The Wheel weaves as the Wheel wills.

  3. #3
    Join Date
    Aug 2006
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStrung::replace problem

    And how about?
    Qt Code:
    1. for (int i=0; i<10 && pos!=-1; ++i) pos = text.indexOf(";",++pos);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.