[Lazarus] Checking characters in a string

Richard Mace richard.mace at gmail.com
Tue Mar 18 07:20:30 CET 2014


Thanks Howard,
Your code worked exactly as I expected.

Richard


On 12 March 2014 16:35, Howard Page-Clark <hdpc at talktalk.net> wrote:

> 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 ---
>
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20140318/96eeb50e/attachment-0003.html>


More information about the Lazarus mailing list