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. Doubts about QString manipulation
Qt 6.11 is out! See what's new in the release blog

Doubts about QString manipulation

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.2k Views 2 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by Swati777999
    #1

    I've written the following code-

    Line 1 : QString myStr = "3.1415161718";
    Line 2 : int sz = myStr.size();  //sz=12
    Line 3:  qDebug()<<"last digit ="<<myStr[sz-1]; // prints -> last digit = '8'
    Line 4: QString last_dig=myStr[sz-1]  //this line gives error
    

    Line 3 works perfectly whereas Line 4 gives an error. So, how can I store the required element of a string in another string?

    Edit:
    I tried the above code with the following condition, it gives me the message Condition satisfied even when the condition is not met.

    if (myStr[sz-1]<5)
       qDebug()<<"Condition satisfied";
    

    I tried to convert it to int and wrote the following code:

    if (myStr[sz-1].toInt()<5)   
       qDebug()<<"Condition satisfied";
    

    This gave me error.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    JKSHJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      @JKSH said in Doubts about QString manipulation:

      @Swati777999 said in Doubts about QString manipulation:

      Line 4: QString last_dig=myStr[sz-1]  //this line gives error
      

      What does the error message say?

      It says no viable conversion from 'QCharRef' to 'QString' .

      S Offline
      S Offline
      Sivan
      wrote on last edited by Sivan
      #5

      @Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do

      int last_digit = myStr[zi-1].digitValue();
      

      Or if you really want them in string

      QString last_digit(myStr[zi-1]);
      
      Swati777999S 1 Reply Last reply
      1
      • Swati777999S Swati777999

        I've written the following code-

        Line 1 : QString myStr = "3.1415161718";
        Line 2 : int sz = myStr.size();  //sz=12
        Line 3:  qDebug()<<"last digit ="<<myStr[sz-1]; // prints -> last digit = '8'
        Line 4: QString last_dig=myStr[sz-1]  //this line gives error
        

        Line 3 works perfectly whereas Line 4 gives an error. So, how can I store the required element of a string in another string?

        Edit:
        I tried the above code with the following condition, it gives me the message Condition satisfied even when the condition is not met.

        if (myStr[sz-1]<5)
           qDebug()<<"Condition satisfied";
        

        I tried to convert it to int and wrote the following code:

        if (myStr[sz-1].toInt()<5)   
           qDebug()<<"Condition satisfied";
        

        This gave me error.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #2

        @Swati777999 said in Doubts about QString manipulation:

        Line 4: QString last_dig=myStr[sz-1]  //this line gives error
        

        What does the error message say?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        Swati777999S 1 Reply Last reply
        0
        • JKSHJ JKSH

          @Swati777999 said in Doubts about QString manipulation:

          Line 4: QString last_dig=myStr[sz-1]  //this line gives error
          

          What does the error message say?

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #3

          @JKSH said in Doubts about QString manipulation:

          @Swati777999 said in Doubts about QString manipulation:

          Line 4: QString last_dig=myStr[sz-1]  //this line gives error
          

          What does the error message say?

          It says no viable conversion from 'QCharRef' to 'QString' .

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          JKSHJ S 2 Replies Last reply
          0
          • Swati777999S Swati777999

            @JKSH said in Doubts about QString manipulation:

            @Swati777999 said in Doubts about QString manipulation:

            Line 4: QString last_dig=myStr[sz-1]  //this line gives error
            

            What does the error message say?

            It says no viable conversion from 'QCharRef' to 'QString' .

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #4

            @Swati777999 said in Doubts about QString manipulation:

            It says no viable conversion from 'QCharRef' to 'QString' .

            https://doc.qt.io/qt-5/qstring.html#QString-8 -- QString constructors can take QChar, but not QCharRef.

            You are using the version of operator[] that returns a QCharRef: https://doc.qt.io/qt-5/qstring.html#operator-5b-5d

            So, you can convert your QCharRef to a QChar first.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            Swati777999S 1 Reply Last reply
            2
            • Swati777999S Swati777999

              @JKSH said in Doubts about QString manipulation:

              @Swati777999 said in Doubts about QString manipulation:

              Line 4: QString last_dig=myStr[sz-1]  //this line gives error
              

              What does the error message say?

              It says no viable conversion from 'QCharRef' to 'QString' .

              S Offline
              S Offline
              Sivan
              wrote on last edited by Sivan
              #5

              @Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do

              int last_digit = myStr[zi-1].digitValue();
              

              Or if you really want them in string

              QString last_digit(myStr[zi-1]);
              
              Swati777999S 1 Reply Last reply
              1
              • JKSHJ JKSH

                @Swati777999 said in Doubts about QString manipulation:

                It says no viable conversion from 'QCharRef' to 'QString' .

                https://doc.qt.io/qt-5/qstring.html#QString-8 -- QString constructors can take QChar, but not QCharRef.

                You are using the version of operator[] that returns a QCharRef: https://doc.qt.io/qt-5/qstring.html#operator-5b-5d

                So, you can convert your QCharRef to a QChar first.

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by
                #6

                @JKSH
                This seems a lil complicated to me. Is not it going to be lengthy?

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                1 Reply Last reply
                0
                • S Sivan

                  @Swati777999 QString's [] operator gives u QChar or QCharRef. You can't assign them straight to a QString. If you want to get the character as digit, you can do

                  int last_digit = myStr[zi-1].digitValue();
                  

                  Or if you really want them in string

                  QString last_digit(myStr[zi-1]);
                  
                  Swati777999S Offline
                  Swati777999S Offline
                  Swati777999
                  wrote on last edited by
                  #7

                  @Sivan Thanks! This worked for me. :)

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  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