Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for MCUs
  4. Delete last character in a string

Delete last character in a string

Scheduled Pinned Locked Moved Unsolved Qt for MCUs
4 Posts 2 Posters 2.0k Views
  • 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.
  • G Offline
    G Offline
    Gaurav Chopra
    wrote on 17 May 2023, 05:11 last edited by
    #1

    How can I delete a last character from a string ?

    S 1 Reply Last reply 17 May 2023, 05:14
    0
    • G Gaurav Chopra
      17 May 2023, 05:11

      How can I delete a last character from a string ?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 17 May 2023, 05:14 last edited by
      #2
      QString string("abc");
      string.chop(1);
      

      https://doc.qt.io/qt-6/qstring.html#chop

      Or, if you prefer const solution:

      const auto shorter = QString("abc").chopped(1);
      

      (Z(:^

      G 1 Reply Last reply 17 May 2023, 05:28
      3
      • S sierdzio
        17 May 2023, 05:14
        QString string("abc");
        string.chop(1);
        

        https://doc.qt.io/qt-6/qstring.html#chop

        Or, if you prefer const solution:

        const auto shorter = QString("abc").chopped(1);
        
        G Offline
        G Offline
        Gaurav Chopra
        wrote on 17 May 2023, 05:28 last edited by
        #3

        @sierdzio Giving error in line 8 ->

        Expected token `,'
        
        import QtQuick 2.0
        
        Rectangle {
            height: 480
            width : 800
            color : "black"
        
        property QString string("abc")
        
        MouseArea{
        
            anchors.fill: parent
            onClicked:{
                string.chop(1);
                console.log(string)
            }
        
        }
        

        }

        S 1 Reply Last reply 17 May 2023, 05:36
        0
        • G Gaurav Chopra
          17 May 2023, 05:28

          @sierdzio Giving error in line 8 ->

          Expected token `,'
          
          import QtQuick 2.0
          
          Rectangle {
              height: 480
              width : 800
              color : "black"
          
          property QString string("abc")
          
          MouseArea{
          
              anchors.fill: parent
              onClicked:{
                  string.chop(1);
                  console.log(string)
              }
          
          }
          

          }

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 17 May 2023, 05:36 last edited by sierdzio
          #4

          Ah, you mean in QML, you should have said so :-)

          In this case you need to look at JavaScript docs for strings. Maybe this will work:

          property string str: "abc"
          onClicked:{
                  str = str.slice(0, -1);
                  console.log(str)
              }
          

          (Z(:^

          1 Reply Last reply
          0

          2/4

          17 May 2023, 05:14

          • Login

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