Link to home
Start Free TrialLog in
Avatar of looknow12
looknow12

asked on

What String function to get numeric values from an alphanumeric field.

What string function can I use to pull only numeric values out of a alphanumeric field.  For example....  'PO # 123', I only want 123.

Is there any switches with the POS function?  Also, what if there are two numbers separated by text?  i.e.  PO# 1234 ggf 455646

Thanks,
Avatar of D-Master
D-Master
Flag of Palestine, State of image

Hi There



function numv(s: string): string;
var
  i: integer;
begin
  result := '';
  for i := 1 to length(s) do
    begin
      if (s[i] in ['1','2','3','4','5','6','7','8','9','0']) then
        begin
          result := result + s[i];
        end;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  edit2.text := numv(edit1.text);
end;
Avatar of _Katka_
Hi, I would suggest you kinda different and more complete version:

program String2Numbers;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TrNUMBER=record
   case IsFloat:Boolean of
    True:(AsFloat:Extended);
    False:(AsInteger:Int64);
  end;

  TaNUMBER=array of TrNUMBER;

function StrToNum(PInput:AnsiString):TaNUMBER;
var
  A,Index:Integer;
  Temp:AnsiString;
  Separated:Boolean;
begin
  Temp:=''; Separated:=False;
  SetLength(Result,0);
  for A:=1 to Length(PInput) do
  begin
    if PInput[A] in ['.','0'..'9'] then
    begin
      if Separated then
      begin
        Separated:=False;
        Index:=Length(Result);
        if Index>0 then
        begin
          if Pos('.',Temp)<>0 then
          begin
            Result[Index-1].IsFloat:=True;
            Result[Index-1].AsFloat:=StrToFloat(Temp);
          end else
          begin
            Result[Index-1].IsFloat:=False;
            Result[Index-1].AsInteger:=StrToInt64(Temp);
          end;
          Temp:='';
        end;
        SetLength(Result,Index+1);
        Temp:=Temp+PInput[A];
      end else Temp:=Temp+PInput[A];
    end else Separated:=True;
  end;
// final detection
  if Pos('.',Temp)<>0 then
  begin
    Result[Index].IsFloat:=True;
    Result[Index].AsFloat:=StrToFloat(Temp);
  end else
  begin
    Result[Index].IsFloat:=False;
    Result[Index].AsInteger:=StrToInt64(Temp);
  end;
end;

var
  A:Integer;
  Result:TaNUMBER;
begin
  DecimalSeparator:='.';
  Result:=StrToNum('PO# 12.34 ggf 455646');
  for A:=0 to Length(Result)-1 do
  if Result[A].IsFloat then WriteLn('Float  : ',Result[A].AsFloat:2:2) else WriteLn('Integer: ',Result[A].AsInteger);
  ReadLn;
end.

Note: this function will return array of numbers, you can determine by IsFloat it's kind and by AsFloat and AsInteger it's value

The current example will return:

Float   : 12.34
Integer: 455646

which in datastructure means:

Result[0] = (IsFloat:True;AsFloat:12.34);
Result[1] = (IsFloat:False;AsInteger:455646);

regards,
Kate
First line of StrToNum has to be rather: "Temp:=''; Separated:=True;"
ASKER CERTIFIED SOLUTION
Avatar of gmayo
gmayo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
heeeeey...that's not fare.... getnums same like numv
Avatar of looknow12
looknow12

ASKER

With one exception.  Getnums detects blanks and segments numeric values into a stringlist.  I found that favorable because it answers the 2nd part of my original question.  

Thank you very much though for your input.  Perhaps I should have split the pts.
no pro bro....