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. remove any int from a string
Forum Updated to NodeBB v4.3 + New Features

remove any int from a string

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 936 Views 5 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.
  • R Ruben12313

    Hello all, quick question

    How can I remove all int numbers from a QString?
    example:

    QString line = ui->comboBox->currenText();
    

    and I get say : Blueberry 2012

    I just want to delete the numbers and store Blueberry.

    Ive tried:
    ```
    line.removeAt('\t');
    line.truncate(9);

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #3

    @Ruben12313
    As @Asperamanca says. Decide on your rules for what exactly constitutes a "number" or "int" and then use QString &QString::replace(const QRegularExpression &re, const QString &after) to replace your target regular expression for the number/int by an empty string. As an example, to remove all sequences of one or more digits, with an optional preceding minus sign, for "int":

    QString s = "Blueberry 2012";
    s.replace(QRegularExpression("-?\\d+"), "");
    

    In your case you might want to include space character in what you remove, else you'll end up with "Blueberry ".

    1 Reply Last reply
    4
    • R Ruben12313

      Hello all, quick question

      How can I remove all int numbers from a QString?
      example:

      QString line = ui->comboBox->currenText();
      

      and I get say : Blueberry 2012

      I just want to delete the numbers and store Blueberry.

      Ive tried:
      ```
      line.removeAt('\t');
      line.truncate(9);

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #4

      @Ruben12313 said in remove any int from a string:

      I get say : Blueberry 2012

      Does the text you get from there always have a similar style like "ABC" + "space/tab" + number?
      Then it might be easier to cut after the separator, as you have tried with removeAt('\t') and you're done.
      Otherwise you have to parse the string without numeric characters (see @JonB 's solution using RegEx)

      Btw: There are no integers in a string. Numeric characters in a string are interpreted as text string and don't have the numeric value they represent.
      "A" directly converted to int is 65 (or 0x41), while
      "65" as numeric string is 54 53 (or 0x36 0x35)

      See "ASCII" and "Integer to String conversion".


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      R 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @Ruben12313 said in remove any int from a string:

        I get say : Blueberry 2012

        Does the text you get from there always have a similar style like "ABC" + "space/tab" + number?
        Then it might be easier to cut after the separator, as you have tried with removeAt('\t') and you're done.
        Otherwise you have to parse the string without numeric characters (see @JonB 's solution using RegEx)

        Btw: There are no integers in a string. Numeric characters in a string are interpreted as text string and don't have the numeric value they represent.
        "A" directly converted to int is 65 (or 0x41), while
        "65" as numeric string is 54 53 (or 0x36 0x35)

        See "ASCII" and "Integer to String conversion".

        R Offline
        R Offline
        Ruben12313
        wrote on last edited by Ruben12313
        #5

        @Pl45m4 yes i only gave blueberry as an example but its a videogame score board:
        (playername 5842) when i used removeAT('\t') it did nothing.

        Pl45m4P 1 Reply Last reply
        0
        • R Ruben12313

          @Pl45m4 yes i only gave blueberry as an example but its a videogame score board:
          (playername 5842) when i used removeAT('\t') it did nothing.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #6

          @Ruben12313 said in remove any int from a string:

          when i used removeAT('\t') it did nothing.

          Because space (' ' ) is not tab (\t).


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • R Offline
            R Offline
            Ruben12313
            wrote on last edited by
            #7

            Hey @Pl45m4 @Asperamanca @JonB

            I went for a for loop but got a slight error : conversion from 'int' to 'QChar' is ambiguous

                auto numb= line1.length();
                for(int i = 0; i < numb; ++i)
                {
                    if (line1.at(i) == 8 )
                    {
                        line1.removeAt(i);
                    }
                }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Hi,

              Why not search from the end of the string for the first white space character ?

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

              R 1 Reply Last reply
              1
              • SGaistS SGaist

                Hi,

                Why not search from the end of the string for the first white space character ?

                R Offline
                R Offline
                Ruben12313
                wrote on last edited by
                #9

                @SGaist said in remove any int from a string:

                search from the end of the string for the first white space character

                Do you have any examples for me to check out? My thing is that in my temp GUI user inputs their name and after the game ends I add the score (lilith k 9876) or (matthew thorn 98739) and I have to update the scores every time they play. So far i can do everything except delete the old score before adding the new one.

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

                  QString::lastIndexOf

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

                  R 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    QString::lastIndexOf

                    R Offline
                    R Offline
                    Ruben12313
                    wrote on last edited by
                    #11

                    @SGaist solved thanks

                        QString name = line1;
                        int pos = name.lastIndexOf(QChar(' '));
                        qDebug() << name.left(pos);
                    
                    1 Reply Last reply
                    0
                    • R Ruben12313 has marked this topic as solved on
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #12

                      Depends how generic you want to do it. If you want to remove all numbers from the entire string, something like this should work (although I'm not sure if it will work for big UTF-8 chars):

                      const auto line = ui->comboBox->currenText();
                      QString result;
                      
                      for (const auto &character : line) {
                        if (character.isDigit() == false) {
                          result.append(character);
                        }
                      }
                      

                      (Z(:^

                      SGaistS 1 Reply Last reply
                      0
                      • sierdzioS sierdzio

                        Depends how generic you want to do it. If you want to remove all numbers from the entire string, something like this should work (although I'm not sure if it will work for big UTF-8 chars):

                        const auto line = ui->comboBox->currenText();
                        QString result;
                        
                        for (const auto &character : line) {
                          if (character.isDigit() == false) {
                            result.append(character);
                          }
                        }
                        
                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @sierdzio this does not take into account player names with numbers in them such as FooBar666 ;-)

                        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
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #14

                          Oh sorry I was looking at some old version of this post :D Forgot to refresh the window, I didn't notice there are other replies already.

                          Yes my code will remove all digits in the entire string.

                          (Z(:^

                          1 Reply Last reply
                          0

                          • Login

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