[Lazarus] Checking characters in a string

Howard Page-Clark hdpc at talktalk.net
Wed Mar 12 17:35:58 CET 2014


On 12/03/2014 13:53, Richard Mace wrote:
> Hi,
> I am trying to check to make sure the a string contains at least 1
> uppercase character, 1 lowercase character and 1 number.
> Any ideas as of the best way of coding this?

If you restrict yourself to Latin ansi encoding and 'number' means a 
numeric digit which does not have to be a whole word, the code is fairly 
simple since almost no parsing is required. Other cases and encodings 
require more complex string parsing. For example:

-- code begin --

function Valid(const s: string): boolean;

   function HasLowerUpperNumeric: boolean;
   var
     c: Char;
     lower: boolean=False;
     upper: boolean=False;
     numeric: boolean=False;
   begin
     Result:=False;
     for c in s do begin
       if c in ['a'..'z'] then
         lower:=True
       else if c in ['A'..'Z'] then
         upper:=True
       else if c in ['0'..'9'] then
         numeric:=True;
       if lower and upper and numeric then
         Exit(True);
     end;
   end;

begin
   if Length(s) < 3 then
     Exit(False);
   Result:=HasLowerUpperNumeric;
end;

-- code end ---





More information about the Lazarus mailing list