[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1755
  • Last Modified:

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,
0
looknow12
Asked:
looknow12
1 Solution
 
D-MasterCommented:
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;
0
 
_Katka_Commented:
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
0
 
_Katka_Commented:
First line of StrToNum has to be rather: "Temp:=''; Separated:=True;"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gmayoCommented:
Add a button, edit box, and memo to your form.

procedure GetNums(input : string; sl : TStringList);
var
      s : string;
      i : integer;
begin
      s := '';
      for i := 1 to Length(input) do begin
            if input[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] then begin
                  s := s + input[i];
            end else begin
                  if s <> '' then sl.Add(s);
                  s := '';
            end;
      end;
      if s <> '' then sl.Add(s);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
      sl : TStringList;
begin
      sl := TStringList.Create;
      GetNums(Edit1.Text, sl);
      Memo1.Clear;
      Memo1.Lines.AddStrings(sl);
      sl.Free;
end;

This will add each numeric sequence as a separate item to the stringlist. Therefore, typing 1abc2def345 into the edit box will yield:
1
2
345

Geoff M.
0
 
D-MasterCommented:
heeeeey...that's not fare.... getnums same like numv
0
 
looknow12Author Commented:
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.
0
 
D-MasterCommented:
no pro bro....
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now