Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved how to trim or normalize a QString?

    General and Desktop
    4
    7
    5816
    Loading More Posts
    • 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
      opengpu last edited by

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

      KroMignon 2 Replies Last reply Reply Quote 0
      • KroMignon
        KroMignon @opengpu last edited by

        @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.Hilk 1 Reply Last reply Reply Quote 1
        • J.Hilk
          J.Hilk Moderators @KroMignon last edited by J.Hilk

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply Reply Quote 4
          • O
            opengpu last edited by

            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.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @opengpu last edited by

              @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

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 1
              • KroMignon
                KroMignon @opengpu last edited by 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.

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

                JKSH 1 Reply Last reply Reply Quote 0
                • JKSH
                  JKSH Moderators @KroMignon last edited by

                  @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 Reply Quote 3
                  • First post
                    Last post