tags:

views:

41

answers:

2

In foxpro is there a function to extract just numbers from a string or variable? Haven't found so far?

A: 

I don't think there is a built-in function. I think you'll need to write a method to loop through your string and use ISDIGIT() to extract your numbers.

Beaner
+4  A: 

You can wrap CHRTRAN functions and get the result in a single line of code. For example:

* This can contain numbers, characters, special characters, etc.
m.lcSource = "ABC.1def23-gHI45J!#6KL"

* This is what I want returned back to me.  In this case, it's digits only.
m.lcReturnToMe = "0123456789"

* The inner CHRTRAN() function removes anything that is a number.  The return value is
* what will be removed in the outer CHRTRAN function.
m.lcDigitsOnly = CHRTRAN(m.lcSource, CHRTRAN(m.lcSource, m.lcReturnToMe, SPACE(0)), SPACE(0))
Frank Perez
This is a technique I've used since forever to also strip out invalid characters from data entry fields....
DRapp
@Frank +1 for this. I thought about it, but isn't this limited to v7 and up?
Beaner
@Beaner I ran this code in FoxPro DOS 2.6 and Visual FoxPro 9. I'll assume it works with everything in between. <grin>
Frank Perez