Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Extract numbers from string

Extract numbers from string

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 8 Posters 0 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.
  • A Anu_Bala

    Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

    int iPanNo;
    CString sNo;
    CString sName = "GROUP23";
    sNo = sName.Right(2);
    iPanNo = atoi(sNo);

    The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

    Anu

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of CString() to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.

    Just say 'NO' to evaluated arguments for diadic functions! Ash

    A Z 2 Replies Last reply
    0
    • A Anu_Bala

      Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

      int iPanNo;
      CString sNo;
      CString sName = "GROUP23";
      sNo = sName.Right(2);
      iPanNo = atoi(sNo);

      The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

      Anu

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #3

      Here is something you can try. You want only the numerals in the string and they appear after the alphabets. First you could use CStringT::SpanExcluding to extract only the alphabets in the string.

      CString sToken = sName.SpanExcluding(L"0123456789");

      Next extract the characters after the length of the alphabets using CStringT::Mid.

      sNo = sName.Mid(sToken.GetLength());

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      1 Reply Last reply
      0
      • A Anu_Bala

        Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

        int iPanNo;
        CString sNo;
        CString sName = "GROUP23";
        sNo = sName.Right(2);
        iPanNo = atoi(sNo);

        The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

        Anu

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #4

        I can see a couple of approaches here. The first way would be to pile your characters in a std::string, use find_first_of to locate where the digits and then use std::stringstream to convert from the digits to a number. That's a bit clunky though. You could go straight to using a std::stringstream, pile the characters in there and then peak at each character extracting non-digits. Once you hit a digit you could just extract the number. Cheers, Ash PS: If you're stuck using CString then use Superman's idea above

        1 Reply Last reply
        0
        • A Anu_Bala

          Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

          int iPanNo;
          CString sNo;
          CString sName = "GROUP23";
          sNo = sName.Right(2);
          iPanNo = atoi(sNo);

          The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

          Anu

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          Anu_Bala wrote:

          Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(), int iPanNo; CString sNo; CString sName = "GROUP23"; sNo = sName.Right(2); iPanNo = atoi(sNo); The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

          Didn't it occur to you that you would have to write some kind of crude loop to do that, at the least? Don't post such lazy assed efforts just to avoid scathing comments. These kinds of posts are far worse.

          ...byte till it megahertz... my donation to web rubbish

          L 1 Reply Last reply
          0
          • A Anu_Bala

            Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

            int iPanNo;
            CString sNo;
            CString sName = "GROUP23";
            sNo = sName.Right(2);
            iPanNo = atoi(sNo);

            The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

            Anu

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #6

            int begin = sName.FindOneOf("0123456789");
            if (begin == -1)
            {
            // handle error
            }
            iPanNo = atoi((LPCSTR)s + begin);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • L Lost User

              You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of CString() to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              A Offline
              A Offline
              Anu_Bala
              wrote on last edited by
              #7

              Thank you.I coded like

              for (int i = 0; i < sNo.GetLength(); i++)
              {
              Number = sNo.GetAt(i);
              if(isdigit(Number))
              res += Number;
              }

              And it works. Thanks.

              Anu

              1 Reply Last reply
              0
              • L Lost User

                You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of CString() to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.

                Just say 'NO' to evaluated arguments for diadic functions! Ash

                Z Offline
                Z Offline
                zhjxin
                wrote on last edited by
                #8

                compare each element of this string to the numeric range '0' to '9', if it's in the range, record it.

                L 1 Reply Last reply
                0
                • L Lost User

                  Anu_Bala wrote:

                  Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(), int iPanNo; CString sNo; CString sName = "GROUP23"; sNo = sName.Right(2); iPanNo = atoi(sNo); The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

                  Didn't it occur to you that you would have to write some kind of crude loop to do that, at the least? Don't post such lazy assed efforts just to avoid scathing comments. These kinds of posts are far worse.

                  ...byte till it megahertz... my donation to web rubbish

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  I think your comments are a tad harsh, particularly your choice of language.

                  Just say 'NO' to evaluated arguments for diadic functions! Ash

                  1 Reply Last reply
                  0
                  • Z zhjxin

                    compare each element of this string to the numeric range '0' to '9', if it's in the range, record it.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Which is exactly what I already said.

                    Just say 'NO' to evaluated arguments for diadic functions! Ash

                    1 Reply Last reply
                    0
                    • A Anu_Bala

                      Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),

                      int iPanNo;
                      CString sNo;
                      CString sName = "GROUP23";
                      sNo = sName.Right(2);
                      iPanNo = atoi(sNo);

                      The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?

                      Anu

                      F Offline
                      F Offline
                      Fareed Rizkalla
                      wrote on last edited by
                      #11

                      Read from right - to - left. Check if digit and set position for the left most digit. Deduct position from length and then you can get the length of your string.

                      N 1 Reply Last reply
                      0
                      • F Fareed Rizkalla

                        Read from right - to - left. Check if digit and set position for the left most digit. Deduct position from length and then you can get the length of your string.

                        N Offline
                        N Offline
                        Niklas L
                        wrote on last edited by
                        #12

                        If you're reading from right to left, you could as well perform the conversion yourself as you pass over each digit.

                        home

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups