How to extract Numbers from a text string ?

ammounpierre used Ask the Experts™
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 ?


Watch Question

Do more with

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

If there is only one number in the string:

teststring='RW( 32)'
FOR CharCount=1 TO LEN(teststring)
      IF ValueFound>0
? ValueFound
Founder, Software Engineer, Data Scientist
? CHRTRAN("RW( 32)", CHRTRAN("RW( 32)","1234567890",""),"")

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
Olaf DoschkeSoftware Developer

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