Results 1 to 5 of 5

Thread: QString::replace difference in behavior

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QString::replace difference in behavior

    Hallo there,

    I have lost sooo much time trying to figure this out and it doesn't make much sense for me!
    The problem is that QString::replace behaves different under Qt5-Windows compared to Qt4-Windows or Qt5-Linux!

    Using:
    ============
    Windows 10 Pro
    21H1
    19043.1165
    Windows Feature Experience Pack 120.2212.3530.0
    ============
    Linux:
    NAME="openSUSE Tumbleweed"
    # VERSION="20210912"
    ID="opensuse-tumbleweed"
    ID_LIKE="opensuse suse"
    VERSION_ID="20210912"
    PRETTY_NAME="openSUSE Tumbleweed"
    ============
    Qt 4.8.7 (only on Windows)
    Qt 5.15.2 (Windows and Linux)
    MSVC 2019 compiler under Windows
    ============

    I just want to change some "special" characters with others. The code I use should be portable, (at least IMHO) there is no magic in it at all.
    But here the code (it is not minimal but runs alone) first to better understand what I want:

    Qt Code:
    1. #include <QDebug>
    2.  
    3. // -----------------------------------------------------------------------------
    4. // -----------------------------------------------------------------------------
    5. void checkStrings(const QString& str1, const QString& str2)
    6. {
    7. if (str1 == str2) {
    8. qDebug() << "=== GOOD ===";
    9. } else {
    10. qDebug() << "### BAD ###";
    11. }
    12. qDebug() << "str1:" << str1;
    13. qDebug() << "str2:" << str2;
    14. }
    15.  
    16. // -----------------------------------------------------------------------------
    17. // -----------------------------------------------------------------------------
    18. void replaceStrings()
    19. {
    20. qDebug() << "\t----> ENTERING: " << Q_FUNC_INFO;
    21. const QString src = "[aabbAABBaabbAABB]";
    22. QString dst = "";
    23. QString tmp = "";
    24.  
    25. tmp = src;
    26. tmp.replace("a", "-");
    27. dst = "[--bbAABB--bbAABB]";
    28. checkStrings(tmp, dst);
    29. }
    30.  
    31. // -----------------------------------------------------------------------------
    32. // -----------------------------------------------------------------------------
    33. void replaceUmlauts()
    34. {
    35. qDebug() << "\t----> ENTERING: " << Q_FUNC_INFO;
    36. const QString src = "[ää,ÄÄ,öö,ÖÖ,üü,ÜÜ]";
    37. QString dst = "";
    38. QString tmp = "";
    39.  
    40. tmp = src;
    41. tmp.replace("ä", "-");
    42. // tmp.replace(QString("ä"), QString("-")); // doesn't matter if we do it this or that way...
    43. dst = "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ]";
    44. checkStrings(tmp, dst);
    45. }
    46.  
    47. // -----------------------------------------------------------------------------
    48. // -----------------------------------------------------------------------------
    49. int main(int argc, char *argv[])
    50. {
    51. Q_UNUSED(argc);
    52. Q_UNUSED(argv);
    53.  
    54. replaceStrings();
    55. replaceUmlauts();
    56.  
    57. return 0;
    58. }
    59.  
    60. // EOF
    To copy to clipboard, switch view to plain text mode 

    The most accurate answer to that should be and is the one under Linux:
    Linux (Qt5) output:

    Qt Code:
    1. ----> ENTERING: void replaceStrings()
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void replaceUmlauts()
    6. === GOOD ===
    7. str1: "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ,--,ÄÄ,öö,ÖÖ,üü,ÜÜ]"
    8. str2: "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ,--,ÄÄ,öö,ÖÖ,üü,ÜÜ]"
    To copy to clipboard, switch view to plain text mode 

    The windows outputs are not giving everything correct back because the console uses codepage 850. But that is OK!
    Windows 10 - Qt4 output:

    Qt Code:
    1. ----> ENTERING: void __cdecl replaceStrings(void)
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void __cdecl replaceUmlauts(void)
    6. === GOOD ===
    7. str1: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    8. str2: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    To copy to clipboard, switch view to plain text mode 

    Windows 10 - Qt5 output:

    Qt Code:
    1. ----> ENTERING: void __cdecl replaceStrings(void)
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void __cdecl replaceUmlauts(void)
    6. ### BAD ###
    7. str1: "[--,--,--,--,--,--,--,--,--,--,--,--]"
    8. str2: "[--,??,??,??,??,??,--,??,??,??,??,??]"
    To copy to clipboard, switch view to plain text mode 

    The problem is that the second compare (line 6) in "replaceUmlauts()" results to "### BAD ###".
    WHYYYYYYYYYYYY is that so???!?!?!?!??!

    Just trying to change:
    the "a"s to "-"s in the first source QString: "[aa,bb,AA,BB,aa,bb,AA,BB]"
    and the "ä"s to "-"s again in the second one "[ää,ÄÄ,öö,ÖÖ,üü,ÜÜ,ää,ÄÄ,öö,ÖÖ, üü,ÜÜ]".

    Thanks for ANY help on that in advance!
    Last edited by freeman_w; 15th September 2021 at 14:32.

Similar Threads

  1. Qt won't recognize QString::replace function.
    By Warumato in forum Newbie
    Replies: 3
    Last Post: 19th March 2020, 18:41
  2. QString unicode replace
    By trallallero in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2012, 15:57
  3. qstring replace backslash by forward-slashes
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 13:07
  4. [solved]QString.replace()
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2011, 10:15
  5. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 21:35

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.