Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread StrToIntDef(), etc.
Fri, Feb 16 2018 11:23 PMPermanent Link

erickengelke


Delphi has a nice StrToIntDef() function which returns a default if the number were not an integer.

StrToInt() is odd because generates an exception if you pass it 'twenty20', but it returns 20 if you pass it '20twenty'.

StrToIntDef( string, default ) would return default in either of the two above cases.

Erick
http://www.erickengelke.com
Sat, Feb 17 2018 12:36 PMPermanent Link

Uli Becker

<<
StrToIntDef( string, default ) would return default in either of the two above cases.
>>

You could write such a function and publish it in a new book.

Uli
Mon, Feb 19 2018 4:11 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> You could write such a function and publish it in a new book.

I figured it was too simple and people would write their own, but here is mine:

var
external NaN : Integer;

function StrToIntDef(szValue : String; nDefault : Integer) : Integer;
begin
   Result := NaN;
   try
       Result := StrToInt(szValue);
   except
   end;
   if isNaN(Result) then
   begin
       if (szValue <> '') and (szValue[1] = '"') then
       begin
           szValue := Copy(szValue, 2, 99);
           if (szValue <> '') and (szValue[Length(szValue)] = '"') then
               szValue := Copy(szValue, 1, Length(szValue) - 1);
           Result := StrToInt(szValue) + 1;
       end;
   end;
   if isNaN(Result) then
   begin
       Result := nDefault;
   end;
end;

What I don't like is that it triggers an exception, because it does the JavaScript one, but that's the way things are unless you do it all manually and that would be slower for all calls.

--

Matthew Jones
Mon, Feb 19 2018 4:34 AMPermanent Link

Michael Dreher

"Matthew Jones" wrote:

//  [...] but here is mine:

And another one here

function StrToIntDef(const s : string; def : integer) : integer;
begin
 if IsInt(s) then
   Result := StrToInt(s)
 else
   Result := def;
end;

See also here

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&msg=2372&start=1&keywords=StrToIntDef&searchbody=True&forum=EWB_Announce&forum=EWB_General&forum=EWB_Components&forum=EWB_Server&forum=EWB_Demos&forum=EWB_Binaries&forum=EWB_Discussion&forum=EWB2_Preview#2372

M. Dreher
Mon, Feb 19 2018 5:03 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

>   if IsInt(s) then

Where is that coming from? Hmm, seems to be undocumented and in webcore unit. Handy - I will have to check that out. Thanks.

(Mine has a more complex extra wart of course, for converting strings enclosed in quotes, which some system gives me for some daft reason...)

--

Matthew Jones
Thu, Feb 22 2018 3:24 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Where is that coming from? Hmm, seems to be undocumented and in webcore unit. Handy - I will have to check that out. Thanks. >>

I will see that these extra functions get documented.  Originally they were just used for parsing, but could be handy for other things.

Tim Young
Elevate Software
www.elevatesoft.com
Image