Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove non-numercial characters from a string 3

Status
Not open for further replies.

mkinn

Programmer
Sep 26, 2003
2
GB
I need to remove non-numerical characters from a string field so that i can convert the field to a number. The non-numeric characters could be anywhere in the string. I could write a lengthy formula using the replace function to remove non-numeric chars but is there a better way of doing this?
 
If you have CR 8 and up you can use the following. Add the line in red if you want the result to be a number and not a string.

stringvar orig:={your.string};
numbervar looper;
numbervar i:=len(orig);
stringvar out;

for looper:= 1 to i do (
if orig[looper] in "0" to "9" then
out:=out & orig [looper] else
out:=out);
out;
tonumber(out)

Mike
 
There were a couple of things i had to change because of nulls and the message "A string can be at most 254 characters long". The formula i ended up with was:

if not(isnull({myfield})) then
(stringvar orig:={myfield};
numbervar looper;
numbervar i:=len(orig);
stringvar out := "";
for looper:= 1 to i do (
if orig[looper] in "0" to "9" then
out:=out & orig [looper] else
out:=out);
if out = "" then
0
else
tonumber(out))
else
0

Thanks for your help - i've been racking my brains over that one for ages!

Mark.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top