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. Can QChar functions work with QStrings ?
Forum Updated to NodeBB v4.3 + New Features

Can QChar functions work with QStrings ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 6 Posters 3.3k Views 1 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by Christian Ehrlicher
    #5

    Sadly QString has no QString::erase()... :(

    #include <QtCore>
    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
        if (argc != 2)
            return 0;
        QString str = app.arguments().at(1);
        const auto checkFunc = [](const QChar c) -> bool
        {
            return c.isLetterOrNumber();
        };
        //str.erase(std::remove_if(str.begin(), str.end(), checkFunc), str.end());
        str.chop(str.end() - std::remove_if(str.begin(), str.end(), checkFunc));
        qDebug() << app.arguments().at(1) << str;
        return 0;
    }
    
    

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    2
    • VRoninV VRonin

      @aha_1980 said in Can QChar functions work with QStrings ?:

      QString has dedicated remove functions with regular expressions that migth do the work faster and in one line.

      True for all latin-alphabet languages, not 100% sure it is safe for the entirety of unicode.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @VRonin said in Can QChar functions work with QStrings ?:

      True for all latin-alphabet languages

      Therefore my question, what the OP really wants to do.

      not 100% sure it is safe for the entirety of unicode.

      https://stackoverflow.com/questions/38001256/handling-accented-letters-in-qregularexpressions-in-qt5

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jefazo92
        wrote on last edited by
        #7

        What I'm trying to do is to check the QString for characters which aren't letters and then delete them. For example, "H?e__llo" would become "Hello".

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jefazo92
          wrote on last edited by jefazo92
          #8

          You are right about isLetterorNumber being redundant I thought it made sense when only isLetter () is what I need. My new code would be:

          for (int i = 0, size = aword.size(); i < size; i++)
                   {
          
                        if (aword[i].isLetter() != 1)
                         {
                            aword.remove(i--, 1);
                            size = aword.size(); 
                         }
          
                   }
          
          1 Reply Last reply
          0
          • J Offline
            J Offline
            jefazo92
            wrote on last edited by jefazo92
            #9

            But I'm still not sure if it'll work. I've read the docs but it says that isLetter() works only for UCS-4 encoded characters but the character of a QString corresponds to one UTF-16 code unit. In that case, I'm not sure if my code would eventually give rise to random behaviour.

            aha_1980A 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @jefazo92 said in Can QChar functions work with QStrings ?:

              works only for UCS-4 encoded characters

              You read the wrong documentation - you're using QChar::isLetter(), not the static QChar::isLetter(uint).

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J jefazo92

                But I'm still not sure if it'll work. I've read the docs but it says that isLetter() works only for UCS-4 encoded characters but the character of a QString corresponds to one UTF-16 code unit. In that case, I'm not sure if my code would eventually give rise to random behaviour.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by aha_1980
                #11

                @jefazo92

                I've read the docs but it says that isLetter() works only for UCS-4 encoded characters

                And that is only correct for this overload, which you are not using. You are using this.

                But: You still didn't tell us what you really want to do. I read your last code, as if you want to remove all non-letter chars from the string.

                Example: "%$Hello/World![" > "HelloWorld" Is that correct?

                Qt has to stay free or it will die.

                1 Reply Last reply
                1
                • J Offline
                  J Offline
                  jefazo92
                  wrote on last edited by
                  #12

                  So what should I do in that case ? I'm a bit lost here.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jefazo92
                    wrote on last edited by
                    #13

                    Yes exactly. That's what I want.

                    aha_1980A 1 Reply Last reply
                    0
                    • J jefazo92

                      Yes exactly. That's what I want.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by aha_1980
                      #14

                      @jefazo92 Then here's a one-liner:

                      #include <QDebug>
                      #include <QRegularExpression>
                      
                      int main(int argc, char *argv[])
                      {
                          QString s = "%$Hello/World![";
                      
                          s.remove(QRegularExpression("[^\\p{L}]"));
                      
                          qDebug() << "s =" << s;
                      }
                      

                      Output: s = "HelloWorld"

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      6
                      • J Offline
                        J Offline
                        jefazo92
                        wrote on last edited by
                        #15

                        @aha_1980 said in Can QChar functions work with QStrings ?:

                        QRegularExpression

                        Would that then remove any punctuation marks, numbers and blank spaces ? Or would I have to add something else ?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jefazo92
                          wrote on last edited by
                          #16

                          Also does that mean that my line above would then not work as it should ?

                          aha_1980A 1 Reply Last reply
                          0
                          • J jefazo92

                            Also does that mean that my line above would then not work as it should ?

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #17

                            @jefazo92

                            Your code should work as well, I'd just clean it up a bit:

                            for (int i = 0; i < aword.size(); i++) {
                                if (!aword[i].isLetter())
                                  aword.remove(i--, 1);
                            }
                            

                            However, why re-invent the wheel if there is a ready-made function?

                            Qt has to stay free or it will die.

                            1 Reply Last reply
                            2
                            • J Offline
                              J Offline
                              jefazo92
                              wrote on last edited by jefazo92
                              #18

                              @aha_1980 said in Can QChar functions work with QStrings ?:

                              QRegularExpression

                              Hi @aha_1980,

                              Thank you a lot for your reply. If it's not too much asking how did you find QRegularExpression ? Like I'm ctrl+F at the QStrings doc but I get 53 coincidences I don't know which of them all is the one that suits me for this example. I'm trying to understand what the [^\p{L}] in the QRegularExpression("[^\p{L}]") means. Also how did you know that the QChar functions I used for QString would work ? Since I'm still a rooky, all these details are important to help me fend off by myself in the future. Thanks.

                              aha_1980A 1 Reply Last reply
                              0
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                You can find the documentation here: QRegularExpression

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                JonBJ 1 Reply Last reply
                                3
                                • Christian EhrlicherC Christian Ehrlicher

                                  You can find the documentation here: QRegularExpression

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #20

                                  @Christian-Ehrlicher
                                  To be fair to @jefazo92, that docs page does not begin to attempt to explain [^\p{L}], or even the \p{L}. In fact it only refers the reader to other sources, such as http://pcre.org/pcre.txt, for all syntax.

                                  I like reg exes, and have used them for years (decades), but it's only right to say to the OP that they are a bit tricky and he'll have to go outside Qt docs to find out about them. A resource I would recommend is an on-line constructor/tester like https://regex101.com/ or https://regexr.com/, though they are not very tutorially.

                                  1 Reply Last reply
                                  3
                                  • J jefazo92

                                    @aha_1980 said in Can QChar functions work with QStrings ?:

                                    QRegularExpression

                                    Hi @aha_1980,

                                    Thank you a lot for your reply. If it's not too much asking how did you find QRegularExpression ? Like I'm ctrl+F at the QStrings doc but I get 53 coincidences I don't know which of them all is the one that suits me for this example. I'm trying to understand what the [^\p{L}] in the QRegularExpression("[^\p{L}]") means. Also how did you know that the QChar functions I used for QString would work ? Since I'm still a rooky, all these details are important to help me fend off by myself in the future. Thanks.

                                    aha_1980A Offline
                                    aha_1980A Offline
                                    aha_1980
                                    Lifetime Qt Champion
                                    wrote on last edited by aha_1980
                                    #21

                                    @jefazo92 said in Can QChar functions work with QStrings ?:

                                    Thank you a lot for your reply. If it's not too much asking how did you find QRegularExpression ? Like I'm ctrl+F at the QStrings doc but I get 53 coincidences I don't know which of them all is the one that suits me for this example.

                                    I'd recomment another way: You know that you want to remove something from your string, so you search for remove first. That gives you some overloads:

                                    QString &	remove(int position, int n)
                                    QString &	remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                    QString &	remove(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                    QString &	remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                    QString &	remove(const QRegExp &rx)
                                    QString &	remove(const QRegularExpression &re)
                                    

                                    The first one simply removes n chars starting at position. The second one removes a specific char from the string. The third and fourth remove a substring. And the last two operate on regular expressions (with QRegExp beeing deprecated).

                                    If you now know that regular expressions are used to describe text patterns, you already have the correct remove overload. Then I googled for qregularexpression non-letter which brought me to https://stackoverflow.com/questions/38001256/handling-accented-letters-in-qregularexpressions-in-qt5

                                    The rest is experience.

                                    I'm trying to understand what the [^\p{L}] in the QRegularExpression("[^\p{L}]") means.

                                    This pattern describes everything that is not ([^]) an unicode letter (\\p{L}). You already got links to regex pages from my mates.

                                    Regards

                                    Qt has to stay free or it will die.

                                    JonBJ 1 Reply Last reply
                                    3
                                    • aha_1980A aha_1980

                                      @jefazo92 said in Can QChar functions work with QStrings ?:

                                      Thank you a lot for your reply. If it's not too much asking how did you find QRegularExpression ? Like I'm ctrl+F at the QStrings doc but I get 53 coincidences I don't know which of them all is the one that suits me for this example.

                                      I'd recomment another way: You know that you want to remove something from your string, so you search for remove first. That gives you some overloads:

                                      QString &	remove(int position, int n)
                                      QString &	remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                      QString &	remove(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                      QString &	remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
                                      QString &	remove(const QRegExp &rx)
                                      QString &	remove(const QRegularExpression &re)
                                      

                                      The first one simply removes n chars starting at position. The second one removes a specific char from the string. The third and fourth remove a substring. And the last two operate on regular expressions (with QRegExp beeing deprecated).

                                      If you now know that regular expressions are used to describe text patterns, you already have the correct remove overload. Then I googled for qregularexpression non-letter which brought me to https://stackoverflow.com/questions/38001256/handling-accented-letters-in-qregularexpressions-in-qt5

                                      The rest is experience.

                                      I'm trying to understand what the [^\p{L}] in the QRegularExpression("[^\p{L}]") means.

                                      This pattern describes everything that is not ([^]) an unicode letter (\\p{L}). You already got links to regex pages from my mates.

                                      Regards

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by JonB
                                      #22

                                      @aha_1980
                                      It's coming across the \p{L} construct for Unicode letter that's hard/intimidating. One does not come across it in many examples, and it's a later addition to reg exes.

                                      1 Reply Last reply
                                      1
                                      • fcarneyF Offline
                                        fcarneyF Offline
                                        fcarney
                                        wrote on last edited by
                                        #23

                                        It bothers me that this ([^\p{L}]) regular expression would work in one engine and fail in another, at least according to the website that was linked:
                                        https://regex101.com/
                                        I tried it in the PHP one and it worked, but the Python and Ecmascript ones failed.

                                        Somehow I had it in my head that regexs were standardized for the most part. That apparently is a bad assumption.

                                        C++ is a perfectly valid school of magic.

                                        aha_1980A JonBJ 2 Replies Last reply
                                        1
                                        • fcarneyF fcarney

                                          It bothers me that this ([^\p{L}]) regular expression would work in one engine and fail in another, at least according to the website that was linked:
                                          https://regex101.com/
                                          I tried it in the PHP one and it worked, but the Python and Ecmascript ones failed.

                                          Somehow I had it in my head that regexs were standardized for the most part. That apparently is a bad assumption.

                                          aha_1980A Offline
                                          aha_1980A Offline
                                          aha_1980
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #24

                                          @fcarney You need a regex engine that supports Unicode (it's a shame that 2019 not all engines do that!), according to: https://www.regular-expressions.info/unicode.html

                                          Regards

                                          Qt has to stay free or it will die.

                                          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