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 can I capture the part of my QString
Forum Updated to NodeBB v4.3 + New Features

How can I capture the part of my QString

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.7k 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.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by
    #1

    Hi All,
    I'm using qt5.5 on my computer and Linux+QT for my OS system.
    I've got a string like this.

    TWO.M TRS-M28\n
    

    And I just need TWO.M TRS-M28 .
    I've seen the QString document.
    I don't see any function that have this ability to do it.
    How can I solve this problem?
    Or any advise?
    Thanks in Advanced!

    J.HilkJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you just want to drop the last character: QString::chop

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        http://doc.qt.io/qt-5/qstring.html#trimmed

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        V 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          If you just want to drop the last character: QString::chop

          V Offline
          V Offline
          victor wang
          wrote on last edited by
          #4

          @SGaist
          Can't I do like this?

          scan_info = scan_info.mid(19).chop(1);
          

          I will get error when I compile.

          mrjjM 1 Reply Last reply
          0
          • V victor wang

            @SGaist
            Can't I do like this?

            scan_info = scan_info.mid(19).chop(1);
            

            I will get error when I compile.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @victor-wang
            Hi

            QString str = " lots\t of\nwhitespace\r\n ";
            str = str.trimmed();
            // str == "lots\t of\nwhitespace"
            or
            scan_info = scan_info.chop(1);

            1 Reply Last reply
            0
            • VRoninV VRonin

              http://doc.qt.io/qt-5/qstring.html#trimmed

              V Offline
              V Offline
              victor wang
              wrote on last edited by
              #6

              @VRonin
              I can do it with this one!

              scan_info =scan_info.mid(19).trimmed();
              

              Thanks

              1 Reply Last reply
              0
              • V victor wang

                Hi All,
                I'm using qt5.5 on my computer and Linux+QT for my OS system.
                I've got a string like this.

                TWO.M TRS-M28\n
                

                And I just need TWO.M TRS-M28 .
                I've seen the QString document.
                I don't see any function that have this ability to do it.
                How can I solve this problem?
                Or any advise?
                Thanks in Advanced!

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

                @victor-wang
                alternatively:

                QString s("TWO.M TRS-M28\n");
                QString removeMe("\n");
                if(s.endsWith(removeMe))
                    s.remove(s.length()-removeMe.length(), removeMe.length());
                

                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.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  If you'd read the documentation of the chop method which also has an example, you would have seen that it modifies the string in-place.

                  By the way, where's that scan_info.mid(19) coming from ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2

                  • Login

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