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 808 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.
  • J Offline
    J Offline
    Juan Dev
    wrote on 22 Jul 2024, 13:19 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...?

    P R 2 Replies Last reply 22 Jul 2024, 13:37
    1
    • J Juan Dev
      22 Jul 2024, 13:19

      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...?

      P Offline
      P Offline
      Pl45m4
      wrote on 22 Jul 2024, 13:37 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

      J 1 Reply Last reply 22 Jul 2024, 14:10
      2
      • P Pl45m4
        22 Jul 2024, 13:37

        @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)

        J Offline
        J Offline
        Juan Dev
        wrote on 22 Jul 2024, 14:10 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 P 2 Replies Last reply 22 Jul 2024, 14:26
        0
        • J Juan Dev
          22 Jul 2024, 13:19

          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...?

          R Offline
          R Offline
          richferrara
          wrote on 22 Jul 2024, 14:20 last edited by
          #4

          @Juan-Dev Try first() instead.

          1 Reply Last reply
          0
          • J Juan Dev
            22 Jul 2024, 14:10

            @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 Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 22 Jul 2024, 14:26 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.

            J 1 Reply Last reply 24 Jul 2024, 13:19
            2
            • J Juan Dev
              22 Jul 2024, 14:10

              @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...?

              P Offline
              P Offline
              Pl45m4
              wrote on 22 Jul 2024, 14:28 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

              J 1 Reply Last reply 24 Jul 2024, 12:50
              1
              • P Pl45m4
                22 Jul 2024, 14:28

                @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;
                
                
                J Offline
                J Offline
                Juan Dev
                wrote on 24 Jul 2024, 12:50 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 24 Jul 2024, 18:05
                0
                • J J.Hilk
                  22 Jul 2024, 14:26

                  @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;
                  }
                  
                  J Offline
                  J Offline
                  Juan Dev
                  wrote on 24 Jul 2024, 13:19 last edited by
                  #8

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

                  P 1 Reply Last reply 24 Jul 2024, 13:23
                  0
                  • J Juan Dev
                    24 Jul 2024, 13:19

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

                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 24 Jul 2024, 13:23 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
                    • J Juan Dev
                      24 Jul 2024, 12:50

                      @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 24 Jul 2024, 18:05 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/

                      J S 2 Replies Last reply 29 Jul 2024, 07:45
                      0
                      • JoeCFDJ JoeCFD
                        24 Jul 2024, 18:05

                        @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/

                        J Offline
                        J Offline
                        Juan Dev
                        wrote on 29 Jul 2024, 07:45 last edited by
                        #11

                        @JoeCFD
                        I think this is another good solution. Thanks

                        1 Reply Last reply
                        0
                        • J Juan Dev has marked this topic as solved on 29 Jul 2024, 07:47
                        • JoeCFDJ JoeCFD
                          24 Jul 2024, 18:05

                          @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 29 Jul 2024, 09:05 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

                          1/12

                          22 Jul 2024, 13:19

                          • Login

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