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 trim or normalize a QString?
QtWS25 Last Chance

how to trim or normalize a QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 9.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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    how to trim or normalize a QString? mainly remove the space and comma.
    eg. " , a,b ,c, d,," to "a,b,c,d"

    KroMignonK 2 Replies Last reply
    0
    • O opengpu

      how to trim or normalize a QString? mainly remove the space and comma.
      eg. " , a,b ,c, d,," to "a,b,c,d"

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @opengpu the easiest way is:

       QString noSpace = myString.split(' ', QString::SkipEmptyParts).join("");
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      J.HilkJ 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @opengpu the easiest way is:

         QString noSpace = myString.split(' ', QString::SkipEmptyParts).join("");
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @KroMignon said in how to trim or normalize a QString?:

        the easiest way is:

        I disagree, the easiest way is to use
        https://doc.qt.io/qt-5/qstring.html#trimmed
        or
        https://doc.qt.io/qt-5/qstring.html#simplified

        ;-)

        edit:
        forgot the comma requirement:
        https://doc.qt.io/qt-5/qstring.html#replace-3


        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
        4
        • O Offline
          O Offline
          opengpu
          wrote on last edited by
          #4

          u mean 1st replace all "," to " ", and then simplified?
          and then split by " ", i will get "a" "b" "c" "d" in the QStringlist, right?

          J.HilkJ 1 Reply Last reply
          0
          • O opengpu

            u mean 1st replace all "," to " ", and then simplified?
            and then split by " ", i will get "a" "b" "c" "d" in the QStringlist, right?

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

            @opengpu
            well yes, and no.

            If your goal is to have all normal chars in a QStringList, than using @KroMignon split with the extra argument is probably faster and easier:

            auto list = myString.split(',' , QString::SkipEmptyParts);


            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
            • O opengpu

              how to trim or normalize a QString? mainly remove the space and comma.
              eg. " , a,b ,c, d,," to "a,b,c,d"

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @opengpu said in how to trim or normalize a QString?:

              mainly remove the space and comma.

              Oups, I think a read/reply too quickly your question: perhaps is should be:

              auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
              

              This will remove all spaces and coma and generate a list with the elements.
              perhaps a better implementation could be

              auto listParts = myString.remove(' ').split(',');
              

              So you could get also empty elements.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              JKSHJ 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @opengpu said in how to trim or normalize a QString?:

                mainly remove the space and comma.

                Oups, I think a read/reply too quickly your question: perhaps is should be:

                auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
                

                This will remove all spaces and coma and generate a list with the elements.
                perhaps a better implementation could be

                auto listParts = myString.remove(' ').split(',');
                

                So you could get also empty elements.

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

                @KroMignon said in how to trim or normalize a QString?:

                auto listParts = myString.split(QRegExp(" |,"), QString::SkipEmptyParts);
                

                QRegExp is deprecated; I suggest QRegularExpression instead.

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

                1 Reply Last reply
                3

                • Login

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