How to extract Numbers from a text string ?

ammounpierre
ammounpierre used Ask the Experts™
on
Hello gurus,

I have a string that contains numbers : example "RW( 32)" OR "ABILF32"

What is the best way to extract the number out of that string ?

thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If there is only one number in the string:

teststring='RW( 32)'
FOR CharCount=1 TO LEN(teststring)
      ValueFound=VAL(SUBSTR(teststring,CharCount))
      IF ValueFound>0
            EXIT
      ENDIF
NEXT
? ValueFound
Founder, Software Engineer, Data Scientist
Commented:
? CHRTRAN("RW( 32)", CHRTRAN("RW( 32)","1234567890",""),"")

Commented:
if you only have one number in the string, otherwise you'll need to decide whether you want to get the first number, last number, or do you want to get a list of all numbers found within the string


string teststring="RW( 32)";

            string strNum = "";
            foreach (char c in teststring)
            {
                if (char.IsNumber(c))
                {
                    strNum += c;
                }
            }

            if (strNum.Length > 0)
            {
                int iNum = int.Parse(strNum);
            }
            else // no number found

Open in new window

And keep in mind you can always lean on the ISDIGIT() function to test characters.

ISDIGIT( ) Function
http://msdn.microsoft.com/en-US/library/0z391s31(v=VS.80).aspx
Olaf DoschkeSoftware Developer

Commented:
There's nothing important to add, Pierre. What are you waiting for?

Bye, Olaf.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial