SRL Archive - SRL Archives

Only() function to check if a string Only has these characters


Ron

2006-12-31 17:35:01

Only() function to check if a string Only has these characters

Function code:

// The Only() function will check a string to see if it only has something.
// Made by Ron. :)
function Only(TheString, OnlyWhat: string; CheckBlank: Boolean): Boolean;
var
i: integer;
begin
TheString := Trim(Lowercase(TheString));
Result := True;
if (CheckBlank) and (Length(TheString) < 1) then
begin
Result := False;
end else
begin
for i := 1 to Length(TheString) do
begin
if (pos(TheString[i], OnlyWhat) < 1) then
begin
Result := False;
Break;
end;
end;
end;
end;

Test the function:

program New;

const
AbcString = 'abcdefg';
NumString = '8273894';

// The Only() function will check a string to see if it only has something.
// Made by Ron. :)
function Only(TheString, OnlyWhat: string; CheckBlank: Boolean): Boolean;
var
i: integer;
begin
TheString := Trim(Lowercase(TheString));
Result := True;
if (CheckBlank) and (Length(TheString) < 1) then
begin
Result := False;
end else
begin
for i := 1 to Length(TheString) do
begin
if (pos(TheString[i], OnlyWhat) < 1) then
begin
Result := False;
Break;
end;
end;
end;
end;

begin
ClearDebug;
if(Only(AbcString,'abcdefghijklmnopqrstuvwxyz',True))then
begin
WriteLn('"'+AbcString+'" has only letters between A and Z');
end;
if(Only(NumString,'1234567890',True))then
begin
WriteLn('"'+NumString+'" has only numbers between 0 and 9');
end;
end.

~Ron