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
Delphi

Avatar of undefined
Last Comment
gskoczylas

8/22/2022 - Mon
SOLUTION
MerijnB

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Sinisa Vuk

This function have little mistake: instead of do use then word in if .... line :-)
MerijnB

lol, yes you're right, tx sinisav :)
aikimark

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
MerijnB

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
gskoczylas

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question