Results 1 to 6 of 6

Thread: change the positions of characters in a string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: change the positions of characters in a string

    Quote Originally Posted by jpn View Post
    However, the first method suggested by Wysota works perfectly.
    So this will work as well:
    Qt Code:
    1. QString str="173562";
    2. QChar *dat = str.data();
    3. qSwap(dat[1], dat[2]);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: change the positions of characters in a string

    The following code will in most likely cause your application to crash:
    Qt Code:
    1. char * str1 = Q_date.toAscii().constData();
    2. swap( str1[0], str1[2] );
    To copy to clipboard, switch view to plain text mode 
    and even if it doesn't it won't work.
    You shouldn't save a pointer to the temporary object returned by toAscii(), it goes out of scope immediately. Go with Wysota's suggestion.

    @jpn + wysota: It's an interesting side effect of Qt's trick with QCharRef that swap doesn't work as expected. I think they should mention QCharRef in the documentation and that it's sometimes more efficient(in this case necessary) to work on the raw QChar array returned by data()

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  2. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11

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
  •  
Qt is a trademark of The Qt Company.