Lazarus

Programming => General => Topic started by: Tz on November 17, 2019, 08:32:44 am

Title: String value for Boolean
Post by: Tz on November 17, 2019, 08:32:44 am
While searhing for configuration input using yes and no, or on and off, for boolean switch

Came a croos this link

https://www.quora.com/In-Java-how-do-I-convert-a-string-with-possible-values-0-1-Yes-No-True-False-into-a-boolean-variable

I wonder this java guy have so many case ? huh  %)

While in FPC we just update the dictionary

Code: Pascal  [Select][+][-]
  1.  
  2.         SetLength(FalseBoolStrs, 9);
  3.         SetLength(TrueBoolStrs,  9);
  4.        
  5.         TrueBoolStrs[0]  := 'True';
  6.         TrueBoolStrs[1]  := 'On';
  7.         TrueBoolStrs[2]  := 'Yes';
  8.         TrueBoolStrs[3]  := 'はい';
  9.         TrueBoolStrs[4]  := 'ใช่';
  10.         TrueBoolStrs[5]  := '是';
  11.         TrueBoolStrs[6]  := '예';
  12.         TrueBoolStrs[7]  := 'כן';
  13.         TrueBoolStrs[8]  := 'نعم';
  14.  
  15.         FalseBoolStrs[0] := 'False';   
  16.         FalseBoolStrs[1] := 'Off';
  17.         FalseBoolStrs[2] := 'No';
  18.         FalseBoolStrs[3] := '番号';  
  19.         FalseBoolStrs[4] := 'ไม่';
  20.         FalseBoolStrs[5] := '没有';
  21.         FalseBoolStrs[6] := '아니';  
  22.         FalseBoolStrs[7] := 'לא';
  23.         FalseBoolStrs[8] := 'لا';
  24.  
  25.         // and voila
  26.         WriteLn(StrToBool('نعم'));
  27.  
  28.  

Thank you FPC you just make my day  :D


* translation is from google, just check out for yourself
Title: Re: String value for Boolean
Post by: Thaddy on November 17, 2019, 09:57:27 am
You can even do this:
Code: Pascal  [Select][+][-]
  1. WriteLn('نعم'.ToBoolean);
Title: Re: String value for Boolean
Post by: jamie on November 17, 2019, 05:56:51 pm
I like this better.
Code: Pascal  [Select][+][-]
  1. Function JToPbool(Const S:string):Boolean;
  2. Const
  3.      Trues:String = ('True,On,Yes,はい,ใช,是,예,כן ,عم');
  4. Begin
  5.   Result := Pos(UpperCase(S), UpperCase(Trues)).ToBoolean;
  6. end;                                                            
  7.  
Title: Re: String value for Boolean
Post by: MarkMLl on November 17, 2019, 06:44:13 pm
Please can I remind anybody doing this of the French insistence on "Oui" and "Non", which can cause real problems if the software applies a "if it's not recognised as the first letter of a 'yes' word it must be 'no'", even where "yes" is the safer case as far as the application program is concerned.

Memories of setting up a graphics card made by Leanord in the early-80s. The messages were in English, but the responses had to be in French.

MarkMLl
Title: Re: String value for Boolean
Post by: Tz on November 17, 2019, 07:16:32 pm
@jamie thanks for remind me for the short,  while everything are false except 'Yes',  could be denial on 'No'

Code: Pascal  [Select][+][-]
  1. program TestStringValueForBoolean;
  2. {$MODE OBJFPC}
  3. {$H+}
  4. uses SysUtils;
  5.  
  6. var
  7.    aBoolean :Boolean = False;
  8.  
  9. begin
  10.  
  11.   TrueBoolStrs  := TStringArray.Create('True', 'On', 'Yes', 'はい', 'ใช่', '是', '예', 'כן', 'نعم');
  12.   FalseBoolStrs := TStringArray.Create('False', 'Off', 'No', '番号', 'ไม่', '没有', '아니', 'לא', 'لا');
  13.  
  14. {
  15.   SetLength(FalseBoolStrs, 9);
  16.   SetLength(TrueBoolStrs,  9);
  17.  
  18.   TrueBoolStrs[0]  := 'True';
  19.   TrueBoolStrs[1]  := 'On';
  20.   TrueBoolStrs[2]  := 'Yes';
  21.   TrueBoolStrs[3]  := 'はい';
  22.   TrueBoolStrs[4]  := 'ใช่';
  23.   TrueBoolStrs[5]  := '是';
  24.   TrueBoolStrs[6]  := '예';
  25.   TrueBoolStrs[7]  := 'כן';
  26.   TrueBoolStrs[8]  := 'نعم';
  27.  
  28.   FalseBoolStrs[0] := 'False';
  29.   FalseBoolStrs[1] := 'Off';
  30.   FalseBoolStrs[2] := 'No';
  31.   FalseBoolStrs[3] := '番号';
  32.   FalseBoolStrs[4] := 'ไม่';
  33.   FalseBoolStrs[5] := '没有';
  34.   FalseBoolStrs[6] := '아니';
  35.   FalseBoolStrs[7] := 'לא';
  36.   FalseBoolStrs[8] := 'لا';
  37. }
  38.  
  39.   if TryStrToBool('1', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  40.   if TryStrToBool('0', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  41.  
  42.   if TryStrToBool('true', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  43.   if TryStrToBool('false', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  44.  
  45.   if TryStrToBool('on', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  46.   if TryStrToBool('off', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  47.  
  48.   if TryStrToBool('yes', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  49.   if TryStrToBool('no', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  50.  
  51.   if TryStrToBool('はい', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  52.   if TryStrToBool('番号', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  53.  
  54.   if TryStrToBool('ใช่', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  55.   if TryStrToBool('ไม่', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  56.  
  57.   if TryStrToBool('是', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  58.   if TryStrToBool('没有', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  59.  
  60.   if TryStrToBool('예', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  61.   if TryStrToBool('아니', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  62.  
  63.   if TryStrToBool('כן', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  64.   if TryStrToBool('לא', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  65.  
  66.   if TryStrToBool('نعم', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  67.   if TryStrToBool('لا', aBoolean) then WriteLn(aBoolean) else WriteLn('error');
  68.  
  69. end.
  70.  
  71.  
Title: Re: String value for Boolean
Post by: winni on November 17, 2019, 08:02:17 pm
Hi!

What about a short and uncomplicated international version

Code: Pascal  [Select][+][-]
  1. const
  2. sFalse ='❌';
  3. sTrue = '✔';
  4.  
  5.  

Winni
TinyPortal © 2005-2018