Link to home
Start Free TrialLog in
Avatar of starhu
starhu

asked on

Delphi : how to know if a string variable contains only a..z, A..Z, 0-9,-, _

Hello,

I have a string variable S.

I need to know  if this S consists only of characters of a..z, A..Z, 0-9,-, _    or it contains other characters as well.

For example:
 'skdfkfkAkd32-k33' would return True
 '5_-kksdfTEkd5-' would return True
'DS3Á323-3kd' would return False
'23Kskdf-k!xx' would return False

I can do it with Pos, but the code would be too long to check every character.
The best solution would be something like regexpin MySql.

Thank you
SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This function have little mistake: instead of do use then word in if .... line :-)
lol, yes you're right, tx sinisav :)
Also, the right parens should be a preceded square bracket character:
if not (str[i] in ['a'..'z', 'A'..'Z', '0'..'9', '-', '_']) then

Open in new window

and actually better to pass the string as const

function CheckString(const str: string): boolean;
var i: integer;
begin
 result := true;
 for i := 1 to Length(str) do
  if not (str[i] in ['a'..'z', 'A'..'Z', '0'..'9', '-', '_']) then
  begin
   result := false;
   exit;
  end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial