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,
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,
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:=T rue;
Result[Index-1].AsFloat:=S trToFloat( Temp);
end else
begin
Result[Index-1].IsFloat:=F alse;
Result[Index-1].AsInteger: =StrToInt6 4(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:=Tru e;
Result[Index].AsFloat:=Str ToFloat(Te mp);
end else
begin
Result[Index].IsFloat:=Fal se;
Result[Index].AsInteger:=S trToInt64( 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.3 4);
Result[1] = (IsFloat:False;AsInteger:4 55646);
regards,
Kate
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
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:=T
Result[Index-1].AsFloat:=S
end else
begin
Result[Index-1].IsFloat:=F
Result[Index-1].AsInteger:
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:=Tru
Result[Index].AsFloat:=Str
end else
begin
Result[Index].IsFloat:=Fal
Result[Index].AsInteger:=S
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.3
Result[1] = (IsFloat:False;AsInteger:4
regards,
Kate
First line of StrToNum has to be rather: "Temp:=''; Separated:=True;"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
heeeeey...that's not fare.... getnums same like numv
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.
Thank you very much though for your input. Perhaps I should have split the pts.
no pro bro....
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','
begin
result := result + s[i];
end;
end;
end;
procedure TForm1.Button1Click(Sender
begin
edit2.text := numv(edit1.text);
end;