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.0k 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.
  • J Offline
    J Offline
    jefazo92
    wrote on 30 Apr 2019, 23:40 last edited by jefazo92
    #1

    Hi everyone,

    I'm trying to code a function which can distinguish letters in strings from numerals
    and punctuation marks. The problem is that I have used QChar function isLetter and isLetterorNumber(). The compiler gives me no problem but I'm not sure if it will give me any random problems when using it. Could someone tell me if it'll be ok and if not how to make it work ? Also I'm not sure if the last line size = aword.size() is required or if it'll work without it. Thanks.

    for (int i = 0, size = aword.size(); i < size; i++)
             {
    
                  if (aword[i].isLetterOrNumber() && aword[i].isLetter())
                   {
                      aword.remove(i--, 1);
                      size = aword.size(); 
                   }
    
             }
    
    A J 2 Replies Last reply 1 May 2019, 04:10
    0
    • J jefazo92
      30 Apr 2019, 23:40

      Hi everyone,

      I'm trying to code a function which can distinguish letters in strings from numerals
      and punctuation marks. The problem is that I have used QChar function isLetter and isLetterorNumber(). The compiler gives me no problem but I'm not sure if it will give me any random problems when using it. Could someone tell me if it'll be ok and if not how to make it work ? Also I'm not sure if the last line size = aword.size() is required or if it'll work without it. Thanks.

      for (int i = 0, size = aword.size(); i < size; i++)
               {
      
                    if (aword[i].isLetterOrNumber() && aword[i].isLetter())
                     {
                        aword.remove(i--, 1);
                        size = aword.size(); 
                     }
      
               }
      
      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 1 May 2019, 04:10 last edited by
      #2

      @jefazo92

      If aword is a QString then yoir code is Ok from compiler side.

      But:

      if (aword[i].isLetterOrNumber() && aword[i].isLetter())

      that looks strange.

      What is your goal, actually? QString has dedicated remove functions with regular expressions that migth do the work faster and in one line.

      Regards

      Qt has to stay free or it will die.

      V 1 Reply Last reply 1 May 2019, 09:27
      3
      • J jefazo92
        30 Apr 2019, 23:40

        Hi everyone,

        I'm trying to code a function which can distinguish letters in strings from numerals
        and punctuation marks. The problem is that I have used QChar function isLetter and isLetterorNumber(). The compiler gives me no problem but I'm not sure if it will give me any random problems when using it. Could someone tell me if it'll be ok and if not how to make it work ? Also I'm not sure if the last line size = aword.size() is required or if it'll work without it. Thanks.

        for (int i = 0, size = aword.size(); i < size; i++)
                 {
        
                      if (aword[i].isLetterOrNumber() && aword[i].isLetter())
                       {
                          aword.remove(i--, 1);
                          size = aword.size(); 
                       }
        
                 }
        
        J Offline
        J Offline
        JonB
        wrote on 1 May 2019, 08:03 last edited by
        #3

        @jefazo92

        Also I'm not sure if the last line size = aword.size() is required or if it'll work without it.

        The way you have written it, with size as a variable, yes, you do need to go either size = aword.size(); or size-- when you remove a character, as the size has changed. Since QString::size() is such a "cheap" operation, you could not bother with a variable and save a line via:

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

        As @aha_1980 has said, you could use a "regular expression" to remove all unwanted characters in a single statement without a loop, though whether at this stage you would find that more complex than what you have is another matter.

        1 Reply Last reply
        1
        • A aha_1980
          1 May 2019, 04:10

          @jefazo92

          If aword is a QString then yoir code is Ok from compiler side.

          But:

          if (aword[i].isLetterOrNumber() && aword[i].isLetter())

          that looks strange.

          What is your goal, actually? QString has dedicated remove functions with regular expressions that migth do the work faster and in one line.

          Regards

          V Offline
          V Offline
          VRonin
          wrote on 1 May 2019, 09:27 last edited by
          #4

          @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.

          "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

          A 1 Reply Last reply 1 May 2019, 10:30
          3
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 1 May 2019, 09:57 last edited by Christian Ehrlicher 5 Jan 2019, 09:59
            #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
            • V VRonin
              1 May 2019, 09:27

              @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.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 1 May 2019, 10:30 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 1 May 2019, 11:47 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 1 May 2019, 11:48 last edited by jefazo92 5 Jan 2019, 11:49
                  #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 1 May 2019, 11:53 last edited by jefazo92 5 Jan 2019, 11:54
                    #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.

                    A 1 Reply Last reply 1 May 2019, 12:00
                    0
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 1 May 2019, 11:59 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
                        1 May 2019, 11:53

                        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.

                        A Offline
                        A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on 1 May 2019, 12:00 last edited by aha_1980 5 Jan 2019, 12:01
                        #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 1 May 2019, 12:00 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 1 May 2019, 12:01 last edited by
                            #13

                            Yes exactly. That's what I want.

                            A 1 Reply Last reply 1 May 2019, 12:08
                            0
                            • J jefazo92
                              1 May 2019, 12:01

                              Yes exactly. That's what I want.

                              A Offline
                              A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on 1 May 2019, 12:08 last edited by aha_1980 5 Jan 2019, 12:09
                              #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 1 May 2019, 12:31 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 1 May 2019, 12:31 last edited by
                                  #16

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

                                  A 1 Reply Last reply 1 May 2019, 12:34
                                  0
                                  • J jefazo92
                                    1 May 2019, 12:31

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

                                    A Offline
                                    A Offline
                                    aha_1980
                                    Lifetime Qt Champion
                                    wrote on 1 May 2019, 12:34 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 1 May 2019, 13:09 last edited by jefazo92 5 Jan 2019, 13:11
                                      #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.

                                      A 1 Reply Last reply 1 May 2019, 13:59
                                      0
                                      • C Online
                                        C Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 1 May 2019, 13:20 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

                                        J 1 Reply Last reply 1 May 2019, 13:48
                                        3
                                        • C Christian Ehrlicher
                                          1 May 2019, 13:20

                                          You can find the documentation here: QRegularExpression

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 1 May 2019, 13:48 last edited by JonB 5 Jan 2019, 13:56
                                          #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

                                          1/25

                                          30 Apr 2019, 23:40

                                          • Login

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