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 830 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.
  • P Pl45m4
    29 Jul 2024, 11:36

    @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 29 Jul 2024, 15:56 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.

    P 1 Reply Last reply 29 Jul 2024, 16:17
    0
    • R Ruben12313
      29 Jul 2024, 15:56

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

      P Online
      P Online
      Pl45m4
      wrote on 29 Jul 2024, 16:17 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 29 Jul 2024, 18:56 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 29 Jul 2024, 19:01 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 29 Jul 2024, 19:16
          1
          • S SGaist
            29 Jul 2024, 19:01

            Hi,

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

            R Offline
            R Offline
            Ruben12313
            wrote on 29 Jul 2024, 19:16 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 Jul 2024, 19:29 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 29 Jul 2024, 20:23
              1
              • S SGaist
                29 Jul 2024, 19:29

                QString::lastIndexOf

                R Offline
                R Offline
                Ruben12313
                wrote on 29 Jul 2024, 20:23 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 29 Jul 2024, 20:25
                • S Offline
                  S Offline
                  sierdzio
                  Moderators
                  wrote on 30 Jul 2024, 05:57 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(:^

                  S 1 Reply Last reply 30 Jul 2024, 07:43
                  0
                  • S sierdzio
                    30 Jul 2024, 05:57

                    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);
                      }
                    }
                    
                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 30 Jul 2024, 07:43 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
                    • S Offline
                      S Offline
                      sierdzio
                      Moderators
                      wrote on 30 Jul 2024, 08:14 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

                      14/14

                      30 Jul 2024, 08:14

                      • Login

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