Thread

programming question (Delphi) (Sdk)

programming question (Delphi) // Sdk

1  |  

tony m

Mar 29, 2001, 12:54am
Anybody know the Delphi equivalent of the VB function "IsNumeric"?

--
- Tony M (a.k.a Tony56) [tony at triton-dynamics.iwarp.com]
"See with your eyes, not with your hands... oh wait, sorry Johnny
Handswitheyes.."
____________________________________________________________

xelag

Mar 30, 2001, 9:36pm
Can you give an example of what IsNumeric does? In Delphi, when I parse a
string and expect an integer, for example, I could use this:

function IsInteger(const s: string): Boolean;
var
n: Integer;
begin
try
n := StrToInt(s);
Result := True;
except
Result := False;
end;
end;

a similar method can be used for others, like for Double etc...
Hope this helps. XelaG

[View Quote]

tony m

Mar 30, 2001, 11:52pm
I'm trying to translate Hal9000's teleports.bas to use with my bot
I'll paste the line where IsNumeric is:

If (IsNumeric(Left(part, 1))) Or (Left(part, 1) = ".") Or
(Left(part, 1) = "-") Then

[View Quote]

xelag

Mar 31, 2001, 7:48am
I've used these for a long time, maybe they are useful to you. Note that I
break the search in ExtractVal if a letter E is found, because E is a sign
for exponential, and is considered by Delphi as part of a number. I think
you could use ExtractNumber.

function ExtractVal(var ss: string; var vv: Double): Boolean;
{ss and v only change if first part of ss contain a Real}
var s: string; i, k: Integer; v: Double;
begin
Result := False;
if Length(ss) = 0 then exit;
k := 1;
while (Pos(ss[k], '.+-') > 0) do begin
inc(k);
if k > Length(ss) then break;
end;
s := ss;
for i := k to Length(s) do begin
try
if CompareText(s[i], 'e') = 0 then break;
v := StrToFloat(Copy(s, 1 , i));
vv := v;
if i < Length(s) then ss := Copy(s, i + 1, Length(s)) else ss :=
'';
Result := True;
except
break;
end;
end;
end;

function ExtractNumber(const a: string;
var a1: string; var nr: Double; var a2:string): Boolean;
// splits string into 3 parts: a1, numeric nr, a2 if successful
var i: Integer; s: string; d: Double;
begin
Result := False;
a1 := a;
nr := 0;
a2 := '';
if Length(a) = 0 then exit;
for i := 1 to Length(a) do begin
s := copy(a, i, Length(a));
if ExtractVal(s, d) then begin
Result := True;
if i > 1 then a1 := Copy(a, 1, i-1) else a1 := '';
nr := d;
a2 := s;
break;
end;
end;
end;



[View Quote]

xelag

Mar 31, 2001, 8:03am
Another interesting one is this one.

'a' is a string you want to split

'sep' is a substring where string 'a' has to be split, it can be a space, or
a comma, or any combination of characters.

'tail' is what is left over after splitting string 'a'

action = 0 if you don't want to 'trim' both string 'a' and 'tail'

if sep is not found, then it returns 'a', and 'tail' is empty.

function ZSplit(a, sep: string; var tail: string; action: Integer): string;
var u, l, lsep: Integer; r: string;
begin
if action > 0 then a := Trim(a);
r := a;
tail := '';
u := Pos(sep, a);
if u > 0 then begin
l := Length(a);
lsep := Length(sep);
r := Copy(a, 1, u-1);
if action > 0 then r := Trim(r);
if u+lsep <= l then tail := Copy(a, u+lsep, l-u-lsep+1);
if action > 0 then tail := Trim(tail);
end;
Result := r;
end;

xelag

Mar 31, 2001, 8:16am
Same one, with uppercase L .... :)

function ZSplit(a, sep: string; var tail: string; action: Integer): string;
var u, L, Lsep: Integer; r: string;
begin
if action > 0 then a := Trim(a);
r := a;
tail := '';
u := Pos(sep, a);
if u > 0 then begin
L := Length(a);
Lsep := Length(sep);
r := Copy(a, 1, u-1);
if action > 0 then r := Trim(r);
if u+Lsep <= L then tail := Copy(a, u+Lsep, L-u-Lsep+1);
if action > 0 then tail := Trim(tail);
end;
Result := r;
end;

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn