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
starhuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MerijnBSr. Software EngineerCommented:
Bluffed this one (means no checks at all)

function CheckString(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', '-', '_') do
  begin
   result := false;
   exit;
  end;
end;

Open in new window

0
Sinisa VukSoftware architectCommented:
This function have little mistake: instead of do use then word in if .... line :-)
0
MerijnBSr. Software EngineerCommented:
lol, yes you're right, tx sinisav :)
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

aikimarkCommented:
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

0
MerijnBSr. Software EngineerCommented:
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

0
gskoczylasSenior Software DeveloperCommented:
In the Delphi 2009 and up instead of operator in it's better to use the standard function CharInSet. For example:
function CheckString(const str: string) : Boolean;
var i: Integer;
begin
  for i := 1 to Length(str) do
    if not CharInSet(str[i], ['a'..'z', 'A'..'Z', '0'..'9', '-', '_']) then
      Exit(False);
  Result := True;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.