1) Her er en funktion jeg engang skrev til at checke om bestemte tegn findes i en streng.
Function Contains(Source: String; Characters: String; ContainsOnly: Boolean): Boolean;
//Actually two functions in one.
{
ContainsOnly = True:
If Source is 'hello world'
and Characters is 'helowrld'
the function will return false, because the space ' ' isnt in the Characters
ContainsOnly = False:
If just one of the characters in Characters is in Source the function returns true.
}
Var
I:Integer;
CharArray: Array[0..255] of Boolean;
Begin
For I := 0 to 255 do CharArray[I] := False; //Set all entries to False, to avoid all the cmp's and jmp's
For I := 1 to Length(Characters) do CharArray[Ord(Characters[I])] := True; //Kinda slow as we evaluate ever character for its value - but it works.
{two differant routines, because of performance}
If ContainsOnly Then
Begin
Result := True; //default.
For I := 1 to Length(Source) do
Begin
If NOT CharArray[Ord(Source[I])] Then
Begin
Result := False;
Exit;
end;
end;
end
else
begin
Result := False;
For I := 1 to Length(Source) do
Begin
If CharArray[Ord(Source[I])] Then
Begin
Result := True;
Exit;
End;
End;
End;
end;
Den kan let ændres til at returnere det tegn der blev fundet istedet for en Boolsk værdi.
Update: Demo projekt lagt op:
http://loftager.dk/udv/Contains.zipMvh,
Thomas Nielsen
[Redigeret d. 31/08-03 10:42:18 af Thomas (Darkstar)]