Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to extract characters from QString...?
Forum Updated to NodeBB v4.3 + New Features

How to extract characters from QString...?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 842 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Juan DevJ Offline
    Juan DevJ Offline
    Juan Dev
    wrote on last edited by
    #1

    I have this simple code to extract the characters and concatenate them to another string.

    QString extract;
    QString stringA = "2024ABCDEF";
    extract = "01-01-";
    extract += stringA.left(4);
    

    And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
    But I read that "leftRef" is deprecated as of QT 6.x

    So I should continue to use left() and not leftRef() and ignore the message...?
    Or use QStringView...?

    Pl45m4P richferraraR 2 Replies Last reply
    1
    • Juan DevJ Juan Dev

      I have this simple code to extract the characters and concatenate them to another string.

      QString extract;
      QString stringA = "2024ABCDEF";
      extract = "01-01-";
      extract += stringA.left(4);
      

      And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
      But I read that "leftRef" is deprecated as of QT 6.x

      So I should continue to use left() and not leftRef() and ignore the message...?
      Or use QStringView...?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Juan-Dev said in How to extract characters from QString...?:

      But I read that "leftRef" is deprecated as of QT 6.x

      The whole QStringRef API/module will be removed in Qt7.

      Or use QStringView...?

      Yes, see what @SGaist wrote here
      (very last comment of the topic. He also linked to the to-be-released Qt6.9 documentation in Qt's dev branch)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Juan DevJ 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @Juan-Dev said in How to extract characters from QString...?:

        But I read that "leftRef" is deprecated as of QT 6.x

        The whole QStringRef API/module will be removed in Qt7.

        Or use QStringView...?

        Yes, see what @SGaist wrote here
        (very last comment of the topic. He also linked to the to-be-released Qt6.9 documentation in Qt's dev branch)

        Juan DevJ Offline
        Juan DevJ Offline
        Juan Dev
        wrote on last edited by
        #3

        @Pl45m4
        First of all, thank you for this response.
        Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
        I can simply display extraction like that :
        qDebug() << QStringView{ stringA }.left(4);
        But how to transform this into QString...?

        J.HilkJ Pl45m4P 2 Replies Last reply
        0
        • Juan DevJ Juan Dev

          I have this simple code to extract the characters and concatenate them to another string.

          QString extract;
          QString stringA = "2024ABCDEF";
          extract = "01-01-";
          extract += stringA.left(4);
          

          And under QT Creator (width QT 5.15.2) I have this message "Use leftRef() instead [clazy-qstring-ref]"
          But I read that "leftRef" is deprecated as of QT 6.x

          So I should continue to use left() and not leftRef() and ignore the message...?
          Or use QStringView...?

          richferraraR Offline
          richferraraR Offline
          richferrara
          wrote on last edited by
          #4

          @Juan-Dev Try first() instead.

          1 Reply Last reply
          0
          • Juan DevJ Juan Dev

            @Pl45m4
            First of all, thank you for this response.
            Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
            I can simply display extraction like that :
            qDebug() << QStringView{ stringA }.left(4);
            But how to transform this into QString...?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Juan-Dev QString has an overload for operator+ that allows for a QStringView object.

            So you don't have to change much at all:

            int main() {
                QString stringA = "2024ABCDEF";
                QStringView stringViewA(stringA);
            
                QString extract = "01-01-";
                extract += stringViewA.left(4);
            
                qDebug() << extract;
            
                return 0;
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            Juan DevJ 1 Reply Last reply
            2
            • Juan DevJ Juan Dev

              @Pl45m4
              First of all, thank you for this response.
              Then I looked at QStringView but if I use my previous code how can I extract characters and concatenate them to a QString ?
              I can simply display extraction like that :
              qDebug() << QStringView{ stringA }.left(4);
              But how to transform this into QString...?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @Juan-Dev said in How to extract characters from QString...?:

              I can simply display extraction like that :
              qDebug() << QStringView{ stringA }.left(4);
              But how to transform this into QString...?

              Isn't the whole point of using QStringView to not have to create additional sub-string variables or refs in the process?

              QString stringA = "ABCDEFGH";
              QStringView abcd = QStringView{ stringA }.left(4);
              // some further string manipulation
              // ...
              
              // make string again once you are done
              QString str = abcd.toString();
              qDebug() << str;
              
              

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              Juan DevJ 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @Juan-Dev said in How to extract characters from QString...?:

                I can simply display extraction like that :
                qDebug() << QStringView{ stringA }.left(4);
                But how to transform this into QString...?

                Isn't the whole point of using QStringView to not have to create additional sub-string variables or refs in the process?

                QString stringA = "ABCDEFGH";
                QStringView abcd = QStringView{ stringA }.left(4);
                // some further string manipulation
                // ...
                
                // make string again once you are done
                QString str = abcd.toString();
                qDebug() << str;
                
                
                Juan DevJ Offline
                Juan DevJ Offline
                Juan Dev
                wrote on last edited by
                #7

                @Pl45m4
                Thank you for your answer. I finally opted for the method provided by @J-Hilk but it is true that it is not necessarily useful to create additional variables with QStringView

                JoeCFDJ 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Juan-Dev QString has an overload for operator+ that allows for a QStringView object.

                  So you don't have to change much at all:

                  int main() {
                      QString stringA = "2024ABCDEF";
                      QStringView stringViewA(stringA);
                  
                      QString extract = "01-01-";
                      extract += stringViewA.left(4);
                  
                      qDebug() << extract;
                  
                      return 0;
                  }
                  
                  Juan DevJ Offline
                  Juan DevJ Offline
                  Juan Dev
                  wrote on last edited by
                  #8

                  @J-Hilk
                  Thank you for your answer :)
                  I hadn't seen that the + operator overload existed.

                  Pl45m4P 1 Reply Last reply
                  0
                  • Juan DevJ Juan Dev

                    @J-Hilk
                    Thank you for your answer :)
                    I hadn't seen that the + operator overload existed.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @Juan-Dev

                    If you are satisfied with the result, please mark this topic as resolved.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    0
                    • Juan DevJ Juan Dev

                      @Pl45m4
                      Thank you for your answer. I finally opted for the method provided by @J-Hilk but it is true that it is not necessarily useful to create additional variables with QStringView

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #10

                      @Juan-Dev
                      Is it easier to use std::string to handle it and convert it to QString when needed? Then Qt version issue will not matter any more.
                      https://cplusplus.com/reference/string/string/substr/

                      Juan DevJ S 2 Replies Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @Juan-Dev
                        Is it easier to use std::string to handle it and convert it to QString when needed? Then Qt version issue will not matter any more.
                        https://cplusplus.com/reference/string/string/substr/

                        Juan DevJ Offline
                        Juan DevJ Offline
                        Juan Dev
                        wrote on last edited by
                        #11

                        @JoeCFD
                        I think this is another good solution. Thanks

                        1 Reply Last reply
                        0
                        • Juan DevJ Juan Dev has marked this topic as solved on
                        • JoeCFDJ JoeCFD

                          @Juan-Dev
                          Is it easier to use std::string to handle it and convert it to QString when needed? Then Qt version issue will not matter any more.
                          https://cplusplus.com/reference/string/string/substr/

                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #12

                          @JoeCFD said in How to extract characters from QString...?:

                          Is it easier to use std::string to handle it and convert it to QString when needed?

                          I had bad experiences with mixing QString and std::string. There have been times (on Windows with MSVC) where some references to std::string could not be found when using QString::toStdString(). Now, we have set the regular C++ locale to be UTF-8 (which means all std::strings are UTF-8). This also means that we need to use QString::fromUtf8(stdstring.c_str()) and qtstring.toUtf8().data() to convert between QString and std::string. (Under some circumstances the conversion from QString to std::string has some weird behaviour because of calling data() on a temporary object. It would be better to store the result of toUtf8() in a QByteArray before.)

                          So, bad experiences with QString::toStdString/fromStdString() together with UTF-8 std::strings tell me it is not easier to go back and forth between std::string and QString. (Also, QString::mid() might have worse performance than QString::midRef(), but certainly better performance than converting back and forth.)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved