1. Computing
GlossaryTips/Tricks|FREE App/VCL|Books|Link Back

Delphi Programming RTL Reference|By Category|Alphabetically|By Unit

StrToBool

unit
SysUtils
category
type conversion routines
declaration
function StrToBool(const s : string): boolean;
description
Returns a boolean from a string value, if *possible*.

While trying to convert a string to a boolean, StrToBool first tries to convert the string into a number; if the number is 0 the function returns False, True otherwise.
If s cannot be converted to a number, True is returned if S equals any of the strings in the TrueBoolStrs array; like wise False is returned if S equals any of the strings in the FalseBoolStrs.

Where FalseBoolStrs and TrueBoolStrs are global Delphi variables (dynamic string arrays) you can set and use when converting a boolean variable to string and back.

example
var s, sT, sF, sError : string;

s      := 'NO'; 
sT     := '2003'; 
sF     := '0'; 
sError := 'Not convertable';

SetLength(TrueBoolStrs,1);
SetLength(FalseBoolStrs,1);

TrueBoolStrs[0]:='YES';
FalseBoolStrs[0]:='NO';

(*
StrToBool(s) == False
StrToBool(sT) == True
StrToBool(sF) == False
StrToBool(sError) == EConvertError exception!!
*)
in real code
see also
TryStrToBool, IntToStr, SetLength, BoolToStr, StrToBoolDef


Free Delphi code snippet inside every Delphi Newsletter!
Subscribe to the Newsletter
Name
Email

Got some code to share? Got a question? Need some help?
See More About

©2014 About.com. All rights reserved.